{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "D:\\Program Files\\Anaconda3\\lib\\importlib\\_bootstrap.py:222: RuntimeWarning: numpy.dtype size changed, may indicate binary incompatibility. Expected 96, got 88\n",
      "  return f(*args, **kwds)\n"
     ]
    }
   ],
   "source": [
    "%matplotlib inline\n",
    "from __future__ import division\n",
    "from numpy.random import randn\n",
    "import numpy as np\n",
    "np.set_printoptions(precision=4, suppress=True)\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from datetime import *"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "DatetimeIndex(['2011-01-01 00:00:00', '2011-01-01 01:00:00',\n",
       "               '2011-01-01 02:00:00', '2011-01-01 03:00:00',\n",
       "               '2011-01-01 04:00:00'],\n",
       "              dtype='datetime64[ns]', freq='H')"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 72 hours starting with midnight Jan 1st, 2011\n",
    "rng = pd.date_range('1/1/2011', periods=72, freq='H')\n",
    "rng[:5]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 132,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2012-01-01   -0.149185\n",
       "2012-01-02   -1.434069\n",
       "2012-01-03   -0.063408\n",
       "Freq: D, dtype: float64"
      ]
     },
     "execution_count": 132,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ts = pd.Series(np.random.randn(len(rng)), index=rng)\n",
    "ts.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "ts.fillna(0,inplace=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2011-01-01 00:00:00   -0.392254\n",
       "2011-01-01 00:45:00   -0.392254\n",
       "2011-01-01 01:30:00   -0.681743\n",
       "2011-01-01 02:15:00   -0.507571\n",
       "2011-01-01 03:00:00    2.632519\n",
       "Freq: 45T, dtype: float64"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# to 45 minute frequency and forward fill\n",
    "converted = ts.asfreq('45Min', method='pad')\n",
    "converted.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2011-01-01   -0.080903\n",
       "2011-01-02    0.119627\n",
       "2011-01-03   -0.134969\n",
       "Freq: D, dtype: float64"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Daily means\n",
    "ts.resample('D').mean()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 19.1 Overview"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 19.2 Timestamps vs. Time Spans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Timestamp('2012-05-01 00:00:00')"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.Timestamp(datetime(2012, 5, 1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Timestamp('2012-05-01 00:00:00')"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.Timestamp('2012-05-01')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Timestamp('2012-05-01 00:00:00')"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.Timestamp(2012, 5, 1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Period('2011-01', 'M')"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.Period('2011-01')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Period('2012-05-01', 'D')"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.Period('2012-05', freq='D')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "dates = [pd.Timestamp('2012-05-01'), pd.Timestamp('2012-05-02'), pd.Timestamp('2012-05-03')]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "pandas.core.indexes.datetimes.DatetimeIndex"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ts = pd.Series(np.random.randn(3), dates)\n",
    "type(ts.index)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "DatetimeIndex(['2012-05-01', '2012-05-02', '2012-05-03'], dtype='datetime64[ns]', freq=None)"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ts.index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2012-05-01   -0.278757\n",
       "2012-05-02    0.842815\n",
       "2012-05-03   -0.153084\n",
       "dtype: float64"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ts"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "periods = [pd.Period('2012-01'), pd.Period('2012-02'), pd.Period('2012-03')]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "ts = pd.Series(np.random.randn(3), periods)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "pandas.core.indexes.period.PeriodIndex"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(ts.index)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "PeriodIndex(['2012-01', '2012-02', '2012-03'], dtype='period[M]', freq='M')"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ts.index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2012-01    0.170503\n",
       "2012-02   -1.920069\n",
       "2012-03   -1.894896\n",
       "Freq: M, dtype: float64"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ts"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 19.3 Converting to Timestamps"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "To convert a Series or list-like object of date-like objects e.g. strings, epochs, or a mixture, you can use the\n",
    "to_datetime function. When passed a Series, this returns a Series (with the same index), while a list-like is\n",
    "converted to a DatetimeIndex:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0   2009-07-31\n",
       "1   2010-01-10\n",
       "2          NaT\n",
       "dtype: datetime64[ns]"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.to_datetime(pd.Series(['Jul 31, 2009', '2010-01-10', None]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "DatetimeIndex(['2005-11-23', '2010-12-31'], dtype='datetime64[ns]', freq=None)"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.to_datetime(['2005/11/23', '2010.12.31'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "DatetimeIndex(['2012-01-04 10:00:00'], dtype='datetime64[ns]', freq=None)"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.to_datetime(['04-01-2012 10:00'], dayfirst=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "DatetimeIndex(['2012-01-14', '2012-01-14'], dtype='datetime64[ns]', freq=None)"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.to_datetime(['14-01-2012', '01-14-2012'], dayfirst=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Timestamp('2010-11-12 00:00:00')"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.to_datetime('2010/11/12')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Timestamp('2010-11-12 00:00:00')"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.Timestamp('2010/11/12')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 19.3.1 Providing a Format Argument"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Timestamp('2010-11-12 00:00:00')"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.to_datetime('2010/11/12', format='%Y/%m/%d')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Timestamp('2010-11-12 00:00:00')"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.to_datetime('12-11-2010 00:00', format='%d-%m-%Y %H:%M')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 19.3.2 Assembling Datetime from Multiple DataFrame Columns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0   2015-02-04 02:00:00\n",
       "1   2016-03-05 03:00:00\n",
       "dtype: datetime64[ns]"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = pd.DataFrame({'year': [2015, 2016],\n",
    "                   'month': [2, 3],\n",
    "                   'day': [4, 5],\n",
    "                   'hour': [2, 3]})\n",
    "pd.to_datetime(df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0   2015-02-04\n",
       "1   2016-03-05\n",
       "dtype: datetime64[ns]"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.to_datetime(df[['year', 'month', 'day']])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 19.3.3 Invalid Data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "ename": "ValueError",
     "evalue": "('Unknown string format:', 'asd')",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[0;32mD:\\Program Files\\Anaconda3\\lib\\site-packages\\pandas\\core\\tools\\datetimes.py\u001b[0m in \u001b[0;36m_convert_listlike\u001b[0;34m(arg, box, format, name, tz)\u001b[0m\n\u001b[1;32m    376\u001b[0m             \u001b[1;32mtry\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m--> 377\u001b[0;31m                 \u001b[0mvalues\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mtz\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mconversion\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mdatetime_to_datetime64\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0marg\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    378\u001b[0m                 \u001b[1;32mreturn\u001b[0m \u001b[0mDatetimeIndex\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_simple_new\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mvalues\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mname\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mname\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mtz\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mtz\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[0;32mpandas\\_libs\\tslibs\\conversion.pyx\u001b[0m in \u001b[0;36mpandas._libs.tslibs.conversion.datetime_to_datetime64\u001b[0;34m()\u001b[0m\n",
      "\u001b[0;31mTypeError\u001b[0m: Unrecognized value type: <class 'str'>",
      "\nDuring handling of the above exception, another exception occurred:\n",
      "\u001b[0;31mValueError\u001b[0m                                Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-46-52baa692ad94>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mpd\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mto_datetime\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m'2009/07/31'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m'asd'\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0merrors\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;34m'raise'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;32mD:\\Program Files\\Anaconda3\\lib\\site-packages\\pandas\\core\\tools\\datetimes.py\u001b[0m in \u001b[0;36mto_datetime\u001b[0;34m(arg, errors, dayfirst, yearfirst, utc, box, format, exact, unit, infer_datetime_format, origin, cache)\u001b[0m\n\u001b[1;32m    465\u001b[0m             \u001b[0mresult\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0m_convert_and_box_cache\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0marg\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mcache_array\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mbox\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0merrors\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m    466\u001b[0m         \u001b[1;32melse\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m--> 467\u001b[0;31m             \u001b[0mresult\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0m_convert_listlike\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0marg\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mbox\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mformat\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    468\u001b[0m     \u001b[1;32melse\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m    469\u001b[0m         \u001b[0mresult\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0m_convert_listlike\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mnp\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0marray\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0marg\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mbox\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mformat\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[0;32mD:\\Program Files\\Anaconda3\\lib\\site-packages\\pandas\\core\\tools\\datetimes.py\u001b[0m in \u001b[0;36m_convert_listlike\u001b[0;34m(arg, box, format, name, tz)\u001b[0m\n\u001b[1;32m    378\u001b[0m                 \u001b[1;32mreturn\u001b[0m \u001b[0mDatetimeIndex\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_simple_new\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mvalues\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mname\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mname\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mtz\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mtz\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m    379\u001b[0m             \u001b[1;32mexcept\u001b[0m \u001b[1;33m(\u001b[0m\u001b[0mValueError\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mTypeError\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m--> 380\u001b[0;31m                 \u001b[1;32mraise\u001b[0m \u001b[0me\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    381\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m    382\u001b[0m     \u001b[1;32mif\u001b[0m \u001b[0marg\u001b[0m \u001b[1;32mis\u001b[0m \u001b[1;32mNone\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[0;32mD:\\Program Files\\Anaconda3\\lib\\site-packages\\pandas\\core\\tools\\datetimes.py\u001b[0m in \u001b[0;36m_convert_listlike\u001b[0;34m(arg, box, format, name, tz)\u001b[0m\n\u001b[1;32m    366\u001b[0m                     \u001b[0mdayfirst\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mdayfirst\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m    367\u001b[0m                     \u001b[0myearfirst\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0myearfirst\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m--> 368\u001b[0;31m                     \u001b[0mrequire_iso8601\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mrequire_iso8601\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    369\u001b[0m                 )\n\u001b[1;32m    370\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[0;32mpandas\\_libs\\tslib.pyx\u001b[0m in \u001b[0;36mpandas._libs.tslib.array_to_datetime\u001b[0;34m()\u001b[0m\n",
      "\u001b[0;32mpandas\\_libs\\tslib.pyx\u001b[0m in \u001b[0;36mpandas._libs.tslib.array_to_datetime\u001b[0;34m()\u001b[0m\n",
      "\u001b[0;32mpandas\\_libs\\tslib.pyx\u001b[0m in \u001b[0;36mpandas._libs.tslib.array_to_datetime\u001b[0;34m()\u001b[0m\n",
      "\u001b[0;32mpandas\\_libs\\tslibs\\parsing.pyx\u001b[0m in \u001b[0;36mpandas._libs.tslibs.parsing.parse_datetime_string\u001b[0;34m()\u001b[0m\n",
      "\u001b[0;32mD:\\Program Files\\Anaconda3\\lib\\site-packages\\dateutil\\parser\\_parser.py\u001b[0m in \u001b[0;36mparse\u001b[0;34m(timestr, parserinfo, **kwargs)\u001b[0m\n\u001b[1;32m   1354\u001b[0m         \u001b[1;32mreturn\u001b[0m \u001b[0mparser\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mparserinfo\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mparse\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mtimestr\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m   1355\u001b[0m     \u001b[1;32melse\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m-> 1356\u001b[0;31m         \u001b[1;32mreturn\u001b[0m \u001b[0mDEFAULTPARSER\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mparse\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mtimestr\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m   1357\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m   1358\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[0;32mD:\\Program Files\\Anaconda3\\lib\\site-packages\\dateutil\\parser\\_parser.py\u001b[0m in \u001b[0;36mparse\u001b[0;34m(self, timestr, default, ignoretz, tzinfos, **kwargs)\u001b[0m\n\u001b[1;32m    646\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m    647\u001b[0m         \u001b[1;32mif\u001b[0m \u001b[0mres\u001b[0m \u001b[1;32mis\u001b[0m \u001b[1;32mNone\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m--> 648\u001b[0;31m             \u001b[1;32mraise\u001b[0m \u001b[0mValueError\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"Unknown string format:\"\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mtimestr\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    649\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m    650\u001b[0m         \u001b[1;32mif\u001b[0m \u001b[0mlen\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mres\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;33m==\u001b[0m \u001b[1;36m0\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[0;31mValueError\u001b[0m: ('Unknown string format:', 'asd')"
     ]
    }
   ],
   "source": [
    "pd.to_datetime(['2009/07/31', 'asd'], errors='raise')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array(['2009/07/31', 'asd'], dtype=object)"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.to_datetime(['2009/07/31', 'asd'], errors='ignore')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "DatetimeIndex(['2009-07-31', 'NaT'], dtype='datetime64[ns]', freq=None)"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.to_datetime(['2009/07/31', 'asd'], errors='coerce')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 19.3.4 Epoch Timestamps"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "DatetimeIndex(['2012-10-08 18:15:05', '2012-10-09 18:15:05',\n",
       "               '2012-10-10 18:15:05', '2012-10-11 18:15:05',\n",
       "               '2012-10-12 18:15:05'],\n",
       "              dtype='datetime64[ns]', freq=None)"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.to_datetime([1349720105, 1349806505, 1349892905,1349979305, 1350065705], unit='s')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "DatetimeIndex(['2012-10-08 18:15:05.100000', '2012-10-08 18:15:05.200000',\n",
       "               '2012-10-08 18:15:05.300000', '2012-10-08 18:15:05.400000',\n",
       "               '2012-10-08 18:15:05.500000'],\n",
       "              dtype='datetime64[ns]', freq=None)"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.to_datetime([1349720105100, 1349720105200, 1349720105300,1349720105400, 1349720105500 ], unit='ms')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 19.3.5 From Timestamps to Epoch"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "DatetimeIndex(['2012-10-08 18:15:05', '2012-10-09 18:15:05',\n",
       "               '2012-10-10 18:15:05', '2012-10-11 18:15:05'],\n",
       "              dtype='datetime64[ns]', freq='D')"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stamps = pd.date_range('2012-10-08 18:15:05', periods=4, freq='D')\n",
    "stamps"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "D:\\Program Files\\Anaconda3\\lib\\site-packages\\ipykernel\\__main__.py:1: FutureWarning: Floor division between integer array and Timedelta is\n",
      "deprecated. Use 'array // timedelta.value' instead.\n",
      "If you want to obtain epochs from an array of timestamps,\n",
      "you can rather use\n",
      "'(array - pd.Timestamp(\"1970-01-01\")) // pd.Timedelta(\"1s\")'.\n",
      "\n",
      "  if __name__ == '__main__':\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([1349720105, 1349806505, 1349892905, 1349979305], dtype=int64)"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stamps.view('int64') // pd.Timedelta(1, unit='s')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 19.3.6 Using the origin Parameter"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "DatetimeIndex(['1960-01-02', '1960-01-03', '1960-01-04'], dtype='datetime64[ns]', freq=None)"
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.to_datetime([1, 2, 3], unit='D', origin=pd.Timestamp('1960-01-01'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "DatetimeIndex(['1970-01-02', '1970-01-03', '1970-01-04'], dtype='datetime64[ns]', freq=None)"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.to_datetime([1, 2, 3], unit='D')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 19.4 Generating Ranges of Timestamps"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "DatetimeIndex(['2012-05-01', '2012-05-02', '2012-05-03'], dtype='datetime64[ns]', freq=None)"
      ]
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dates = [datetime(2012, 5, 1), datetime(2012, 5, 2), datetime(2012, 5, 3)]\n",
    "# Note the frequency information\n",
    "index = pd.DatetimeIndex(dates)\n",
    "index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "DatetimeIndex(['2012-05-01', '2012-05-02', '2012-05-03'], dtype='datetime64[ns]', freq=None)"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Automatically converted to DatetimeIndex\n",
    "index = pd.Index(dates)\n",
    "index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 139,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "DatetimeIndex(['2011-01-01', '2011-01-02', '2011-01-03', '2011-01-04',\n",
       "               '2011-01-05', '2011-01-06', '2011-01-07', '2011-01-08',\n",
       "               '2011-01-09', '2011-01-10',\n",
       "               ...\n",
       "               '2011-12-23', '2011-12-24', '2011-12-25', '2011-12-26',\n",
       "               '2011-12-27', '2011-12-28', '2011-12-29', '2011-12-30',\n",
       "               '2011-12-31', '2012-01-01'],\n",
       "              dtype='datetime64[ns]', length=366, freq='D')"
      ]
     },
     "execution_count": 139,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "start = datetime(2011, 1, 1)\n",
    "end = datetime(2012, 1, 1)\n",
    "index = pd.date_range(start, end)\n",
    "index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 140,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "DatetimeIndex(['2011-01-03', '2011-01-04', '2011-01-05', '2011-01-06',\n",
       "               '2011-01-07', '2011-01-10', '2011-01-11', '2011-01-12',\n",
       "               '2011-01-13', '2011-01-14',\n",
       "               ...\n",
       "               '2011-12-19', '2011-12-20', '2011-12-21', '2011-12-22',\n",
       "               '2011-12-23', '2011-12-26', '2011-12-27', '2011-12-28',\n",
       "               '2011-12-29', '2011-12-30'],\n",
       "              dtype='datetime64[ns]', length=260, freq='B')"
      ]
     },
     "execution_count": 140,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "index = pd.bdate_range(start, end)\n",
    "index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "DatetimeIndex(['2011-01-31', '2011-02-28', '2011-03-31', '2011-04-30',\n",
       "               '2011-05-31', '2011-06-30', '2011-07-31', '2011-08-31',\n",
       "               '2011-09-30', '2011-10-31',\n",
       "               ...\n",
       "               '2093-07-31', '2093-08-31', '2093-09-30', '2093-10-31',\n",
       "               '2093-11-30', '2093-12-31', '2094-01-31', '2094-02-28',\n",
       "               '2094-03-31', '2094-04-30'],\n",
       "              dtype='datetime64[ns]', length=1000, freq='M')"
      ]
     },
     "execution_count": 63,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.date_range(start, periods=1000, freq='M')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "DatetimeIndex(['2011-01-03', '2011-04-01', '2011-07-01', '2011-10-03',\n",
       "               '2012-01-02', '2012-04-02', '2012-07-02', '2012-10-01',\n",
       "               '2013-01-01', '2013-04-01',\n",
       "               ...\n",
       "               '2071-01-01', '2071-04-01', '2071-07-01', '2071-10-01',\n",
       "               '2072-01-01', '2072-04-01', '2072-07-01', '2072-10-03',\n",
       "               '2073-01-02', '2073-04-03'],\n",
       "              dtype='datetime64[ns]', length=250, freq='BQS-JAN')"
      ]
     },
     "execution_count": 64,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.bdate_range(start, periods=250, freq='BQS')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "date_range and bdate_range make it easy to generate a range of dates using various combinations of parame-\n",
    "ters like start, end, periods, and freq. The start and end dates are strictly inclusive, so dates outside of those\n",
    "specified will not be generated:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 141,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "DatetimeIndex(['2011-01-31', '2011-02-28', '2011-03-31', '2011-04-29',\n",
       "               '2011-05-31', '2011-06-30', '2011-07-29', '2011-08-31',\n",
       "               '2011-09-30', '2011-10-31', '2011-11-30', '2011-12-30'],\n",
       "              dtype='datetime64[ns]', freq='BM')"
      ]
     },
     "execution_count": 141,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.date_range(start, end, freq='BM')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "DatetimeIndex(['2011-01-02', '2011-01-09', '2011-01-16', '2011-01-23',\n",
       "               '2011-01-30', '2011-02-06', '2011-02-13', '2011-02-20',\n",
       "               '2011-02-27', '2011-03-06', '2011-03-13', '2011-03-20',\n",
       "               '2011-03-27', '2011-04-03', '2011-04-10', '2011-04-17',\n",
       "               '2011-04-24', '2011-05-01', '2011-05-08', '2011-05-15',\n",
       "               '2011-05-22', '2011-05-29', '2011-06-05', '2011-06-12',\n",
       "               '2011-06-19', '2011-06-26', '2011-07-03', '2011-07-10',\n",
       "               '2011-07-17', '2011-07-24', '2011-07-31', '2011-08-07',\n",
       "               '2011-08-14', '2011-08-21', '2011-08-28', '2011-09-04',\n",
       "               '2011-09-11', '2011-09-18', '2011-09-25', '2011-10-02',\n",
       "               '2011-10-09', '2011-10-16', '2011-10-23', '2011-10-30',\n",
       "               '2011-11-06', '2011-11-13', '2011-11-20', '2011-11-27',\n",
       "               '2011-12-04', '2011-12-11', '2011-12-18', '2011-12-25',\n",
       "               '2012-01-01'],\n",
       "              dtype='datetime64[ns]', freq='W-SUN')"
      ]
     },
     "execution_count": 66,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.date_range(start, end, freq='W')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "DatetimeIndex(['2011-01-02', '2011-01-09', '2011-01-16', '2011-01-23',\n",
       "               '2011-01-30', '2011-02-06', '2011-02-13', '2011-02-20',\n",
       "               '2011-02-27', '2011-03-06', '2011-03-13', '2011-03-20',\n",
       "               '2011-03-27', '2011-04-03', '2011-04-10', '2011-04-17',\n",
       "               '2011-04-24', '2011-05-01', '2011-05-08', '2011-05-15',\n",
       "               '2011-05-22', '2011-05-29', '2011-06-05', '2011-06-12',\n",
       "               '2011-06-19', '2011-06-26', '2011-07-03', '2011-07-10',\n",
       "               '2011-07-17', '2011-07-24', '2011-07-31', '2011-08-07',\n",
       "               '2011-08-14', '2011-08-21', '2011-08-28', '2011-09-04',\n",
       "               '2011-09-11', '2011-09-18', '2011-09-25', '2011-10-02',\n",
       "               '2011-10-09', '2011-10-16', '2011-10-23', '2011-10-30',\n",
       "               '2011-11-06', '2011-11-13', '2011-11-20', '2011-11-27',\n",
       "               '2011-12-04', '2011-12-11', '2011-12-18', '2011-12-25',\n",
       "               '2012-01-01'],\n",
       "              dtype='datetime64[ns]', freq='W-SUN')"
      ]
     },
     "execution_count": 67,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.date_range(start, end, freq='W')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "DatetimeIndex(['2011-12-05', '2011-12-06', '2011-12-07', '2011-12-08',\n",
       "               '2011-12-09', '2011-12-12', '2011-12-13', '2011-12-14',\n",
       "               '2011-12-15', '2011-12-16', '2011-12-19', '2011-12-20',\n",
       "               '2011-12-21', '2011-12-22', '2011-12-23', '2011-12-26',\n",
       "               '2011-12-27', '2011-12-28', '2011-12-29', '2011-12-30'],\n",
       "              dtype='datetime64[ns]', freq='B')"
      ]
     },
     "execution_count": 68,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.bdate_range(end=end, periods=20)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "DatetimeIndex(['2011-01-03', '2011-01-04', '2011-01-05', '2011-01-06',\n",
       "               '2011-01-07', '2011-01-10', '2011-01-11', '2011-01-12',\n",
       "               '2011-01-13', '2011-01-14', '2011-01-17', '2011-01-18',\n",
       "               '2011-01-19', '2011-01-20', '2011-01-21', '2011-01-24',\n",
       "               '2011-01-25', '2011-01-26', '2011-01-27', '2011-01-28'],\n",
       "              dtype='datetime64[ns]', freq='B')"
      ]
     },
     "execution_count": 69,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.bdate_range(start=start, periods=20)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 19.4.1 Custom Frequency Ranges"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "weekmask = 'Mon Wed Fri'\n",
    "holidays = [datetime(2011, 1, 5), datetime(2011, 3, 14)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "DatetimeIndex(['2011-01-03', '2011-01-07', '2011-01-10', '2011-01-12',\n",
       "               '2011-01-14', '2011-01-17', '2011-01-19', '2011-01-21',\n",
       "               '2011-01-24', '2011-01-26',\n",
       "               ...\n",
       "               '2011-12-09', '2011-12-12', '2011-12-14', '2011-12-16',\n",
       "               '2011-12-19', '2011-12-21', '2011-12-23', '2011-12-26',\n",
       "               '2011-12-28', '2011-12-30'],\n",
       "              dtype='datetime64[ns]', length=154, freq='C')"
      ]
     },
     "execution_count": 72,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.bdate_range(start, end, freq='C', weekmask=weekmask, holidays=holidays)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "DatetimeIndex(['2011-01-03', '2011-02-02', '2011-03-02', '2011-04-01',\n",
       "               '2011-05-02', '2011-06-01', '2011-07-01', '2011-08-01',\n",
       "               '2011-09-02', '2011-10-03', '2011-11-02', '2011-12-02'],\n",
       "              dtype='datetime64[ns]', freq='CBMS')"
      ]
     },
     "execution_count": 73,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.bdate_range(start, end, freq='CBMS', weekmask=weekmask)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 19.5 Timestamp Limitations"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Timestamp('1677-09-21 00:12:43.145225')"
      ]
     },
     "execution_count": 75,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.Timestamp.min"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Timestamp('2262-04-11 23:47:16.854775807')"
      ]
     },
     "execution_count": 76,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.Timestamp.max"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 19.6 Indexing"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "One of the main uses for DatetimeIndex is as an index for pandas objects. The DatetimeIndex class contains\n",
    "many time series related optimizations:\n",
    "- A large range of dates for various offsets are pre-computed and cached under the hood in order to make generating subsequent date ranges very fast (just have to grab a slice)\n",
    "- Fast shifting using the shift and tshift method on pandas objects\n",
    "- Unioning of overlapping DatetimeIndex objects with the same frequency is very fast (important for fast data alignment)\n",
    "- Quick access to date fields via properties such as year, month, etc.\n",
    "- Regularization functions like snap and very fast asof logic\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 142,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "DatetimeIndex(['2011-01-31', '2011-02-28', '2011-03-31', '2011-04-29',\n",
       "               '2011-05-31', '2011-06-30', '2011-07-29', '2011-08-31',\n",
       "               '2011-09-30', '2011-10-31', '2011-11-30', '2011-12-30'],\n",
       "              dtype='datetime64[ns]', freq='BM')"
      ]
     },
     "execution_count": 142,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rng = pd.date_range(start, end, freq='BM')\n",
    "ts = pd.Series(np.random.randn(len(rng)), index=rng)\n",
    "ts.index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 143,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "DatetimeIndex(['2011-01-31', '2011-02-28', '2011-03-31', '2011-04-29',\n",
       "               '2011-05-31'],\n",
       "              dtype='datetime64[ns]', freq='BM')"
      ]
     },
     "execution_count": 143,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ts[:5].index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "DatetimeIndex(['2011-01-31', '2011-03-31', '2011-05-31', '2011-07-29',\n",
       "               '2011-09-30', '2011-11-30'],\n",
       "              dtype='datetime64[ns]', freq='2BM')"
      ]
     },
     "execution_count": 80,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ts[::2].index"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 19.6.1 Partial String Indexing"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-0.2512852432060748"
      ]
     },
     "execution_count": 81,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ts['1/31/2011']"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "时间范围索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2011-12-30    0.126305\n",
       "Freq: BM, dtype: float64"
      ]
     },
     "execution_count": 82,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ts[datetime(2011, 12, 25):]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2011-10-31    0.403783\n",
       "2011-11-30   -1.141530\n",
       "2011-12-30    0.126305\n",
       "Freq: BM, dtype: float64"
      ]
     },
     "execution_count": 83,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ts['10/31/2011':'12/31/2011']"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "To provide convenience for accessing longer time series, you can also pass in the year or year and month as strings:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 84,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2011-01-31   -0.251285\n",
       "2011-02-28    0.281436\n",
       "2011-03-31   -0.478295\n",
       "2011-04-29    2.214053\n",
       "2011-05-31   -1.558014\n",
       "2011-06-30    0.131650\n",
       "2011-07-29   -1.683818\n",
       "2011-08-31   -1.827492\n",
       "2011-09-30   -0.032326\n",
       "2011-10-31    0.403783\n",
       "2011-11-30   -1.141530\n",
       "2011-12-30    0.126305\n",
       "Freq: BM, dtype: float64"
      ]
     },
     "execution_count": 84,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ts['2011']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 85,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2011-06-30    0.13165\n",
       "Freq: BM, dtype: float64"
      ]
     },
     "execution_count": 85,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ts['2011-6']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:00:00</th>\n",
       "      <td>0.159180</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:01:00</th>\n",
       "      <td>-0.371462</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:02:00</th>\n",
       "      <td>0.454213</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:03:00</th>\n",
       "      <td>0.711532</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:04:00</th>\n",
       "      <td>-0.223610</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:05:00</th>\n",
       "      <td>0.834068</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:06:00</th>\n",
       "      <td>-0.048895</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:07:00</th>\n",
       "      <td>-0.591174</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:08:00</th>\n",
       "      <td>-1.535174</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:09:00</th>\n",
       "      <td>1.178111</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:10:00</th>\n",
       "      <td>-0.775582</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:11:00</th>\n",
       "      <td>-0.210906</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:12:00</th>\n",
       "      <td>0.766672</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:13:00</th>\n",
       "      <td>0.933659</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:14:00</th>\n",
       "      <td>1.853951</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:15:00</th>\n",
       "      <td>0.740702</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:16:00</th>\n",
       "      <td>0.591871</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:17:00</th>\n",
       "      <td>1.736773</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:18:00</th>\n",
       "      <td>1.153951</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:19:00</th>\n",
       "      <td>1.982074</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:20:00</th>\n",
       "      <td>-0.294754</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:21:00</th>\n",
       "      <td>1.175221</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:22:00</th>\n",
       "      <td>-0.629549</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:23:00</th>\n",
       "      <td>0.917592</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:24:00</th>\n",
       "      <td>-1.562463</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:25:00</th>\n",
       "      <td>-0.123375</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:26:00</th>\n",
       "      <td>-0.740918</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:27:00</th>\n",
       "      <td>-1.033469</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:28:00</th>\n",
       "      <td>0.060992</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:29:00</th>\n",
       "      <td>-0.862869</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-03-11 10:10:00</th>\n",
       "      <td>-0.768855</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-03-11 10:11:00</th>\n",
       "      <td>1.499428</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-03-11 10:12:00</th>\n",
       "      <td>0.331957</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-03-11 10:13:00</th>\n",
       "      <td>-0.436550</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-03-11 10:14:00</th>\n",
       "      <td>0.929643</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-03-11 10:15:00</th>\n",
       "      <td>0.422351</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-03-11 10:16:00</th>\n",
       "      <td>0.099351</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-03-11 10:17:00</th>\n",
       "      <td>-0.879476</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-03-11 10:18:00</th>\n",
       "      <td>-1.302739</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-03-11 10:19:00</th>\n",
       "      <td>0.439142</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-03-11 10:20:00</th>\n",
       "      <td>0.777119</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-03-11 10:21:00</th>\n",
       "      <td>1.604349</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-03-11 10:22:00</th>\n",
       "      <td>-0.808042</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-03-11 10:23:00</th>\n",
       "      <td>0.536151</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-03-11 10:24:00</th>\n",
       "      <td>-0.600579</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-03-11 10:25:00</th>\n",
       "      <td>-0.070631</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-03-11 10:26:00</th>\n",
       "      <td>-0.027033</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-03-11 10:27:00</th>\n",
       "      <td>-0.800949</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-03-11 10:28:00</th>\n",
       "      <td>0.842324</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-03-11 10:29:00</th>\n",
       "      <td>1.900294</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-03-11 10:30:00</th>\n",
       "      <td>1.174173</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-03-11 10:31:00</th>\n",
       "      <td>0.864070</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-03-11 10:32:00</th>\n",
       "      <td>0.264151</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-03-11 10:33:00</th>\n",
       "      <td>-0.471017</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-03-11 10:34:00</th>\n",
       "      <td>1.879504</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-03-11 10:35:00</th>\n",
       "      <td>0.078067</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-03-11 10:36:00</th>\n",
       "      <td>-0.286081</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-03-11 10:37:00</th>\n",
       "      <td>0.295102</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-03-11 10:38:00</th>\n",
       "      <td>-0.357536</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-03-11 10:39:00</th>\n",
       "      <td>1.949904</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>100000 rows × 1 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                            A\n",
       "2013-01-01 00:00:00  0.159180\n",
       "2013-01-01 00:01:00 -0.371462\n",
       "2013-01-01 00:02:00  0.454213\n",
       "2013-01-01 00:03:00  0.711532\n",
       "2013-01-01 00:04:00 -0.223610\n",
       "2013-01-01 00:05:00  0.834068\n",
       "2013-01-01 00:06:00 -0.048895\n",
       "2013-01-01 00:07:00 -0.591174\n",
       "2013-01-01 00:08:00 -1.535174\n",
       "2013-01-01 00:09:00  1.178111\n",
       "2013-01-01 00:10:00 -0.775582\n",
       "2013-01-01 00:11:00 -0.210906\n",
       "2013-01-01 00:12:00  0.766672\n",
       "2013-01-01 00:13:00  0.933659\n",
       "2013-01-01 00:14:00  1.853951\n",
       "2013-01-01 00:15:00  0.740702\n",
       "2013-01-01 00:16:00  0.591871\n",
       "2013-01-01 00:17:00  1.736773\n",
       "2013-01-01 00:18:00  1.153951\n",
       "2013-01-01 00:19:00  1.982074\n",
       "2013-01-01 00:20:00 -0.294754\n",
       "2013-01-01 00:21:00  1.175221\n",
       "2013-01-01 00:22:00 -0.629549\n",
       "2013-01-01 00:23:00  0.917592\n",
       "2013-01-01 00:24:00 -1.562463\n",
       "2013-01-01 00:25:00 -0.123375\n",
       "2013-01-01 00:26:00 -0.740918\n",
       "2013-01-01 00:27:00 -1.033469\n",
       "2013-01-01 00:28:00  0.060992\n",
       "2013-01-01 00:29:00 -0.862869\n",
       "...                       ...\n",
       "2013-03-11 10:10:00 -0.768855\n",
       "2013-03-11 10:11:00  1.499428\n",
       "2013-03-11 10:12:00  0.331957\n",
       "2013-03-11 10:13:00 -0.436550\n",
       "2013-03-11 10:14:00  0.929643\n",
       "2013-03-11 10:15:00  0.422351\n",
       "2013-03-11 10:16:00  0.099351\n",
       "2013-03-11 10:17:00 -0.879476\n",
       "2013-03-11 10:18:00 -1.302739\n",
       "2013-03-11 10:19:00  0.439142\n",
       "2013-03-11 10:20:00  0.777119\n",
       "2013-03-11 10:21:00  1.604349\n",
       "2013-03-11 10:22:00 -0.808042\n",
       "2013-03-11 10:23:00  0.536151\n",
       "2013-03-11 10:24:00 -0.600579\n",
       "2013-03-11 10:25:00 -0.070631\n",
       "2013-03-11 10:26:00 -0.027033\n",
       "2013-03-11 10:27:00 -0.800949\n",
       "2013-03-11 10:28:00  0.842324\n",
       "2013-03-11 10:29:00  1.900294\n",
       "2013-03-11 10:30:00  1.174173\n",
       "2013-03-11 10:31:00  0.864070\n",
       "2013-03-11 10:32:00  0.264151\n",
       "2013-03-11 10:33:00 -0.471017\n",
       "2013-03-11 10:34:00  1.879504\n",
       "2013-03-11 10:35:00  0.078067\n",
       "2013-03-11 10:36:00 -0.286081\n",
       "2013-03-11 10:37:00  0.295102\n",
       "2013-03-11 10:38:00 -0.357536\n",
       "2013-03-11 10:39:00  1.949904\n",
       "\n",
       "[100000 rows x 1 columns]"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dft = pd.DataFrame(randn(100000,1),columns=['A'],index=pd.date_range('20130101',periods=100000,freq='T'))\n",
    "dft"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 88,
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:00:00</th>\n",
       "      <td>0.237964</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:01:00</th>\n",
       "      <td>0.227762</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:02:00</th>\n",
       "      <td>-0.041934</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:03:00</th>\n",
       "      <td>0.623022</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:04:00</th>\n",
       "      <td>-0.520355</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:05:00</th>\n",
       "      <td>0.411469</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:06:00</th>\n",
       "      <td>0.338686</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:07:00</th>\n",
       "      <td>0.612961</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:08:00</th>\n",
       "      <td>0.930348</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:09:00</th>\n",
       "      <td>-1.145872</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:10:00</th>\n",
       "      <td>1.824010</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:11:00</th>\n",
       "      <td>-1.901515</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:12:00</th>\n",
       "      <td>-1.906472</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:13:00</th>\n",
       "      <td>-1.342346</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:14:00</th>\n",
       "      <td>-0.565809</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:15:00</th>\n",
       "      <td>-0.611475</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:16:00</th>\n",
       "      <td>1.001233</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:17:00</th>\n",
       "      <td>0.795559</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:18:00</th>\n",
       "      <td>0.364509</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:19:00</th>\n",
       "      <td>-0.761569</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:20:00</th>\n",
       "      <td>1.388499</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:21:00</th>\n",
       "      <td>2.271801</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:22:00</th>\n",
       "      <td>0.559616</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:23:00</th>\n",
       "      <td>0.559026</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:24:00</th>\n",
       "      <td>-0.434898</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:25:00</th>\n",
       "      <td>-1.192385</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:26:00</th>\n",
       "      <td>0.255456</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:27:00</th>\n",
       "      <td>-0.661277</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:28:00</th>\n",
       "      <td>0.152875</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:29:00</th>\n",
       "      <td>0.326932</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-03-11 10:10:00</th>\n",
       "      <td>0.293315</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-03-11 10:11:00</th>\n",
       "      <td>-0.265512</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-03-11 10:12:00</th>\n",
       "      <td>-1.348597</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-03-11 10:13:00</th>\n",
       "      <td>-0.218638</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-03-11 10:14:00</th>\n",
       "      <td>0.484193</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-03-11 10:15:00</th>\n",
       "      <td>-1.101798</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-03-11 10:16:00</th>\n",
       "      <td>2.558371</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-03-11 10:17:00</th>\n",
       "      <td>0.224214</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-03-11 10:18:00</th>\n",
       "      <td>-0.266649</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-03-11 10:19:00</th>\n",
       "      <td>-2.434019</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-03-11 10:20:00</th>\n",
       "      <td>-0.553213</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-03-11 10:21:00</th>\n",
       "      <td>1.416233</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-03-11 10:22:00</th>\n",
       "      <td>-1.308558</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-03-11 10:23:00</th>\n",
       "      <td>-0.527849</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-03-11 10:24:00</th>\n",
       "      <td>1.174169</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-03-11 10:25:00</th>\n",
       "      <td>-1.221344</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-03-11 10:26:00</th>\n",
       "      <td>0.112709</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-03-11 10:27:00</th>\n",
       "      <td>-0.572001</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-03-11 10:28:00</th>\n",
       "      <td>1.673150</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-03-11 10:29:00</th>\n",
       "      <td>-1.122375</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-03-11 10:30:00</th>\n",
       "      <td>-2.027510</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-03-11 10:31:00</th>\n",
       "      <td>-0.101700</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-03-11 10:32:00</th>\n",
       "      <td>0.178712</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-03-11 10:33:00</th>\n",
       "      <td>-0.611314</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-03-11 10:34:00</th>\n",
       "      <td>1.993286</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-03-11 10:35:00</th>\n",
       "      <td>-2.062802</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-03-11 10:36:00</th>\n",
       "      <td>0.596053</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-03-11 10:37:00</th>\n",
       "      <td>0.769460</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-03-11 10:38:00</th>\n",
       "      <td>0.021056</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-03-11 10:39:00</th>\n",
       "      <td>0.878949</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>100000 rows × 1 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                            A\n",
       "2013-01-01 00:00:00  0.237964\n",
       "2013-01-01 00:01:00  0.227762\n",
       "2013-01-01 00:02:00 -0.041934\n",
       "2013-01-01 00:03:00  0.623022\n",
       "2013-01-01 00:04:00 -0.520355\n",
       "2013-01-01 00:05:00  0.411469\n",
       "2013-01-01 00:06:00  0.338686\n",
       "2013-01-01 00:07:00  0.612961\n",
       "2013-01-01 00:08:00  0.930348\n",
       "2013-01-01 00:09:00 -1.145872\n",
       "2013-01-01 00:10:00  1.824010\n",
       "2013-01-01 00:11:00 -1.901515\n",
       "2013-01-01 00:12:00 -1.906472\n",
       "2013-01-01 00:13:00 -1.342346\n",
       "2013-01-01 00:14:00 -0.565809\n",
       "2013-01-01 00:15:00 -0.611475\n",
       "2013-01-01 00:16:00  1.001233\n",
       "2013-01-01 00:17:00  0.795559\n",
       "2013-01-01 00:18:00  0.364509\n",
       "2013-01-01 00:19:00 -0.761569\n",
       "2013-01-01 00:20:00  1.388499\n",
       "2013-01-01 00:21:00  2.271801\n",
       "2013-01-01 00:22:00  0.559616\n",
       "2013-01-01 00:23:00  0.559026\n",
       "2013-01-01 00:24:00 -0.434898\n",
       "2013-01-01 00:25:00 -1.192385\n",
       "2013-01-01 00:26:00  0.255456\n",
       "2013-01-01 00:27:00 -0.661277\n",
       "2013-01-01 00:28:00  0.152875\n",
       "2013-01-01 00:29:00  0.326932\n",
       "...                       ...\n",
       "2013-03-11 10:10:00  0.293315\n",
       "2013-03-11 10:11:00 -0.265512\n",
       "2013-03-11 10:12:00 -1.348597\n",
       "2013-03-11 10:13:00 -0.218638\n",
       "2013-03-11 10:14:00  0.484193\n",
       "2013-03-11 10:15:00 -1.101798\n",
       "2013-03-11 10:16:00  2.558371\n",
       "2013-03-11 10:17:00  0.224214\n",
       "2013-03-11 10:18:00 -0.266649\n",
       "2013-03-11 10:19:00 -2.434019\n",
       "2013-03-11 10:20:00 -0.553213\n",
       "2013-03-11 10:21:00  1.416233\n",
       "2013-03-11 10:22:00 -1.308558\n",
       "2013-03-11 10:23:00 -0.527849\n",
       "2013-03-11 10:24:00  1.174169\n",
       "2013-03-11 10:25:00 -1.221344\n",
       "2013-03-11 10:26:00  0.112709\n",
       "2013-03-11 10:27:00 -0.572001\n",
       "2013-03-11 10:28:00  1.673150\n",
       "2013-03-11 10:29:00 -1.122375\n",
       "2013-03-11 10:30:00 -2.027510\n",
       "2013-03-11 10:31:00 -0.101700\n",
       "2013-03-11 10:32:00  0.178712\n",
       "2013-03-11 10:33:00 -0.611314\n",
       "2013-03-11 10:34:00  1.993286\n",
       "2013-03-11 10:35:00 -2.062802\n",
       "2013-03-11 10:36:00  0.596053\n",
       "2013-03-11 10:37:00  0.769460\n",
       "2013-03-11 10:38:00  0.021056\n",
       "2013-03-11 10:39:00  0.878949\n",
       "\n",
       "[100000 rows x 1 columns]"
      ]
     },
     "execution_count": 88,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dft['2013']"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This starts on the very first time in the month, and includes the last date & time for the month"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 89,
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:00:00</th>\n",
       "      <td>0.237964</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:01:00</th>\n",
       "      <td>0.227762</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:02:00</th>\n",
       "      <td>-0.041934</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:03:00</th>\n",
       "      <td>0.623022</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:04:00</th>\n",
       "      <td>-0.520355</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:05:00</th>\n",
       "      <td>0.411469</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:06:00</th>\n",
       "      <td>0.338686</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:07:00</th>\n",
       "      <td>0.612961</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:08:00</th>\n",
       "      <td>0.930348</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:09:00</th>\n",
       "      <td>-1.145872</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:10:00</th>\n",
       "      <td>1.824010</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:11:00</th>\n",
       "      <td>-1.901515</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:12:00</th>\n",
       "      <td>-1.906472</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:13:00</th>\n",
       "      <td>-1.342346</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:14:00</th>\n",
       "      <td>-0.565809</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:15:00</th>\n",
       "      <td>-0.611475</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:16:00</th>\n",
       "      <td>1.001233</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:17:00</th>\n",
       "      <td>0.795559</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:18:00</th>\n",
       "      <td>0.364509</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:19:00</th>\n",
       "      <td>-0.761569</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:20:00</th>\n",
       "      <td>1.388499</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:21:00</th>\n",
       "      <td>2.271801</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:22:00</th>\n",
       "      <td>0.559616</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:23:00</th>\n",
       "      <td>0.559026</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:24:00</th>\n",
       "      <td>-0.434898</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:25:00</th>\n",
       "      <td>-1.192385</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:26:00</th>\n",
       "      <td>0.255456</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:27:00</th>\n",
       "      <td>-0.661277</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:28:00</th>\n",
       "      <td>0.152875</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:29:00</th>\n",
       "      <td>0.326932</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-02-28 23:30:00</th>\n",
       "      <td>0.221781</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-02-28 23:31:00</th>\n",
       "      <td>-0.818396</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-02-28 23:32:00</th>\n",
       "      <td>0.076940</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-02-28 23:33:00</th>\n",
       "      <td>-0.094153</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-02-28 23:34:00</th>\n",
       "      <td>-0.444643</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-02-28 23:35:00</th>\n",
       "      <td>-0.164524</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-02-28 23:36:00</th>\n",
       "      <td>0.438759</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-02-28 23:37:00</th>\n",
       "      <td>0.288757</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-02-28 23:38:00</th>\n",
       "      <td>0.070976</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-02-28 23:39:00</th>\n",
       "      <td>-0.428153</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-02-28 23:40:00</th>\n",
       "      <td>0.314524</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-02-28 23:41:00</th>\n",
       "      <td>-0.785011</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-02-28 23:42:00</th>\n",
       "      <td>1.070809</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-02-28 23:43:00</th>\n",
       "      <td>0.101327</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-02-28 23:44:00</th>\n",
       "      <td>0.354671</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-02-28 23:45:00</th>\n",
       "      <td>0.382417</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-02-28 23:46:00</th>\n",
       "      <td>0.113086</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-02-28 23:47:00</th>\n",
       "      <td>-0.368498</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-02-28 23:48:00</th>\n",
       "      <td>1.157715</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-02-28 23:49:00</th>\n",
       "      <td>-0.203778</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-02-28 23:50:00</th>\n",
       "      <td>0.097099</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-02-28 23:51:00</th>\n",
       "      <td>-0.059900</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-02-28 23:52:00</th>\n",
       "      <td>0.941902</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-02-28 23:53:00</th>\n",
       "      <td>-0.556070</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-02-28 23:54:00</th>\n",
       "      <td>-1.189051</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-02-28 23:55:00</th>\n",
       "      <td>-1.353019</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-02-28 23:56:00</th>\n",
       "      <td>-1.005935</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-02-28 23:57:00</th>\n",
       "      <td>-0.575245</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-02-28 23:58:00</th>\n",
       "      <td>0.539861</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-02-28 23:59:00</th>\n",
       "      <td>0.407656</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>84960 rows × 1 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                            A\n",
       "2013-01-01 00:00:00  0.237964\n",
       "2013-01-01 00:01:00  0.227762\n",
       "2013-01-01 00:02:00 -0.041934\n",
       "2013-01-01 00:03:00  0.623022\n",
       "2013-01-01 00:04:00 -0.520355\n",
       "2013-01-01 00:05:00  0.411469\n",
       "2013-01-01 00:06:00  0.338686\n",
       "2013-01-01 00:07:00  0.612961\n",
       "2013-01-01 00:08:00  0.930348\n",
       "2013-01-01 00:09:00 -1.145872\n",
       "2013-01-01 00:10:00  1.824010\n",
       "2013-01-01 00:11:00 -1.901515\n",
       "2013-01-01 00:12:00 -1.906472\n",
       "2013-01-01 00:13:00 -1.342346\n",
       "2013-01-01 00:14:00 -0.565809\n",
       "2013-01-01 00:15:00 -0.611475\n",
       "2013-01-01 00:16:00  1.001233\n",
       "2013-01-01 00:17:00  0.795559\n",
       "2013-01-01 00:18:00  0.364509\n",
       "2013-01-01 00:19:00 -0.761569\n",
       "2013-01-01 00:20:00  1.388499\n",
       "2013-01-01 00:21:00  2.271801\n",
       "2013-01-01 00:22:00  0.559616\n",
       "2013-01-01 00:23:00  0.559026\n",
       "2013-01-01 00:24:00 -0.434898\n",
       "2013-01-01 00:25:00 -1.192385\n",
       "2013-01-01 00:26:00  0.255456\n",
       "2013-01-01 00:27:00 -0.661277\n",
       "2013-01-01 00:28:00  0.152875\n",
       "2013-01-01 00:29:00  0.326932\n",
       "...                       ...\n",
       "2013-02-28 23:30:00  0.221781\n",
       "2013-02-28 23:31:00 -0.818396\n",
       "2013-02-28 23:32:00  0.076940\n",
       "2013-02-28 23:33:00 -0.094153\n",
       "2013-02-28 23:34:00 -0.444643\n",
       "2013-02-28 23:35:00 -0.164524\n",
       "2013-02-28 23:36:00  0.438759\n",
       "2013-02-28 23:37:00  0.288757\n",
       "2013-02-28 23:38:00  0.070976\n",
       "2013-02-28 23:39:00 -0.428153\n",
       "2013-02-28 23:40:00  0.314524\n",
       "2013-02-28 23:41:00 -0.785011\n",
       "2013-02-28 23:42:00  1.070809\n",
       "2013-02-28 23:43:00  0.101327\n",
       "2013-02-28 23:44:00  0.354671\n",
       "2013-02-28 23:45:00  0.382417\n",
       "2013-02-28 23:46:00  0.113086\n",
       "2013-02-28 23:47:00 -0.368498\n",
       "2013-02-28 23:48:00  1.157715\n",
       "2013-02-28 23:49:00 -0.203778\n",
       "2013-02-28 23:50:00  0.097099\n",
       "2013-02-28 23:51:00 -0.059900\n",
       "2013-02-28 23:52:00  0.941902\n",
       "2013-02-28 23:53:00 -0.556070\n",
       "2013-02-28 23:54:00 -1.189051\n",
       "2013-02-28 23:55:00 -1.353019\n",
       "2013-02-28 23:56:00 -1.005935\n",
       "2013-02-28 23:57:00 -0.575245\n",
       "2013-02-28 23:58:00  0.539861\n",
       "2013-02-28 23:59:00  0.407656\n",
       "\n",
       "[84960 rows x 1 columns]"
      ]
     },
     "execution_count": 89,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dft['2013-1':'2013-2']"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This specifies a stop time that includes all of the times on the last day"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 90,
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:00:00</th>\n",
       "      <td>0.237964</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:01:00</th>\n",
       "      <td>0.227762</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:02:00</th>\n",
       "      <td>-0.041934</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:03:00</th>\n",
       "      <td>0.623022</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:04:00</th>\n",
       "      <td>-0.520355</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:05:00</th>\n",
       "      <td>0.411469</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:06:00</th>\n",
       "      <td>0.338686</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:07:00</th>\n",
       "      <td>0.612961</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:08:00</th>\n",
       "      <td>0.930348</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:09:00</th>\n",
       "      <td>-1.145872</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:10:00</th>\n",
       "      <td>1.824010</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:11:00</th>\n",
       "      <td>-1.901515</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:12:00</th>\n",
       "      <td>-1.906472</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:13:00</th>\n",
       "      <td>-1.342346</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:14:00</th>\n",
       "      <td>-0.565809</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:15:00</th>\n",
       "      <td>-0.611475</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:16:00</th>\n",
       "      <td>1.001233</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:17:00</th>\n",
       "      <td>0.795559</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:18:00</th>\n",
       "      <td>0.364509</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:19:00</th>\n",
       "      <td>-0.761569</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:20:00</th>\n",
       "      <td>1.388499</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:21:00</th>\n",
       "      <td>2.271801</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:22:00</th>\n",
       "      <td>0.559616</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:23:00</th>\n",
       "      <td>0.559026</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:24:00</th>\n",
       "      <td>-0.434898</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:25:00</th>\n",
       "      <td>-1.192385</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:26:00</th>\n",
       "      <td>0.255456</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:27:00</th>\n",
       "      <td>-0.661277</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:28:00</th>\n",
       "      <td>0.152875</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:29:00</th>\n",
       "      <td>0.326932</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-02-28 23:30:00</th>\n",
       "      <td>0.221781</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-02-28 23:31:00</th>\n",
       "      <td>-0.818396</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-02-28 23:32:00</th>\n",
       "      <td>0.076940</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-02-28 23:33:00</th>\n",
       "      <td>-0.094153</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-02-28 23:34:00</th>\n",
       "      <td>-0.444643</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-02-28 23:35:00</th>\n",
       "      <td>-0.164524</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-02-28 23:36:00</th>\n",
       "      <td>0.438759</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-02-28 23:37:00</th>\n",
       "      <td>0.288757</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-02-28 23:38:00</th>\n",
       "      <td>0.070976</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-02-28 23:39:00</th>\n",
       "      <td>-0.428153</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-02-28 23:40:00</th>\n",
       "      <td>0.314524</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-02-28 23:41:00</th>\n",
       "      <td>-0.785011</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-02-28 23:42:00</th>\n",
       "      <td>1.070809</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-02-28 23:43:00</th>\n",
       "      <td>0.101327</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-02-28 23:44:00</th>\n",
       "      <td>0.354671</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-02-28 23:45:00</th>\n",
       "      <td>0.382417</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-02-28 23:46:00</th>\n",
       "      <td>0.113086</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-02-28 23:47:00</th>\n",
       "      <td>-0.368498</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-02-28 23:48:00</th>\n",
       "      <td>1.157715</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-02-28 23:49:00</th>\n",
       "      <td>-0.203778</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-02-28 23:50:00</th>\n",
       "      <td>0.097099</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-02-28 23:51:00</th>\n",
       "      <td>-0.059900</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-02-28 23:52:00</th>\n",
       "      <td>0.941902</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-02-28 23:53:00</th>\n",
       "      <td>-0.556070</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-02-28 23:54:00</th>\n",
       "      <td>-1.189051</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-02-28 23:55:00</th>\n",
       "      <td>-1.353019</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-02-28 23:56:00</th>\n",
       "      <td>-1.005935</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-02-28 23:57:00</th>\n",
       "      <td>-0.575245</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-02-28 23:58:00</th>\n",
       "      <td>0.539861</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-02-28 23:59:00</th>\n",
       "      <td>0.407656</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>84960 rows × 1 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                            A\n",
       "2013-01-01 00:00:00  0.237964\n",
       "2013-01-01 00:01:00  0.227762\n",
       "2013-01-01 00:02:00 -0.041934\n",
       "2013-01-01 00:03:00  0.623022\n",
       "2013-01-01 00:04:00 -0.520355\n",
       "2013-01-01 00:05:00  0.411469\n",
       "2013-01-01 00:06:00  0.338686\n",
       "2013-01-01 00:07:00  0.612961\n",
       "2013-01-01 00:08:00  0.930348\n",
       "2013-01-01 00:09:00 -1.145872\n",
       "2013-01-01 00:10:00  1.824010\n",
       "2013-01-01 00:11:00 -1.901515\n",
       "2013-01-01 00:12:00 -1.906472\n",
       "2013-01-01 00:13:00 -1.342346\n",
       "2013-01-01 00:14:00 -0.565809\n",
       "2013-01-01 00:15:00 -0.611475\n",
       "2013-01-01 00:16:00  1.001233\n",
       "2013-01-01 00:17:00  0.795559\n",
       "2013-01-01 00:18:00  0.364509\n",
       "2013-01-01 00:19:00 -0.761569\n",
       "2013-01-01 00:20:00  1.388499\n",
       "2013-01-01 00:21:00  2.271801\n",
       "2013-01-01 00:22:00  0.559616\n",
       "2013-01-01 00:23:00  0.559026\n",
       "2013-01-01 00:24:00 -0.434898\n",
       "2013-01-01 00:25:00 -1.192385\n",
       "2013-01-01 00:26:00  0.255456\n",
       "2013-01-01 00:27:00 -0.661277\n",
       "2013-01-01 00:28:00  0.152875\n",
       "2013-01-01 00:29:00  0.326932\n",
       "...                       ...\n",
       "2013-02-28 23:30:00  0.221781\n",
       "2013-02-28 23:31:00 -0.818396\n",
       "2013-02-28 23:32:00  0.076940\n",
       "2013-02-28 23:33:00 -0.094153\n",
       "2013-02-28 23:34:00 -0.444643\n",
       "2013-02-28 23:35:00 -0.164524\n",
       "2013-02-28 23:36:00  0.438759\n",
       "2013-02-28 23:37:00  0.288757\n",
       "2013-02-28 23:38:00  0.070976\n",
       "2013-02-28 23:39:00 -0.428153\n",
       "2013-02-28 23:40:00  0.314524\n",
       "2013-02-28 23:41:00 -0.785011\n",
       "2013-02-28 23:42:00  1.070809\n",
       "2013-02-28 23:43:00  0.101327\n",
       "2013-02-28 23:44:00  0.354671\n",
       "2013-02-28 23:45:00  0.382417\n",
       "2013-02-28 23:46:00  0.113086\n",
       "2013-02-28 23:47:00 -0.368498\n",
       "2013-02-28 23:48:00  1.157715\n",
       "2013-02-28 23:49:00 -0.203778\n",
       "2013-02-28 23:50:00  0.097099\n",
       "2013-02-28 23:51:00 -0.059900\n",
       "2013-02-28 23:52:00  0.941902\n",
       "2013-02-28 23:53:00 -0.556070\n",
       "2013-02-28 23:54:00 -1.189051\n",
       "2013-02-28 23:55:00 -1.353019\n",
       "2013-02-28 23:56:00 -1.005935\n",
       "2013-02-28 23:57:00 -0.575245\n",
       "2013-02-28 23:58:00  0.539861\n",
       "2013-02-28 23:59:00  0.407656\n",
       "\n",
       "[84960 rows x 1 columns]"
      ]
     },
     "execution_count": 90,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dft['2013-1':'2013-2-28']"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This specifies an exact stop time (and is not the same as the above)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 91,
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:00:00</th>\n",
       "      <td>0.237964</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:01:00</th>\n",
       "      <td>0.227762</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:02:00</th>\n",
       "      <td>-0.041934</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:03:00</th>\n",
       "      <td>0.623022</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:04:00</th>\n",
       "      <td>-0.520355</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:05:00</th>\n",
       "      <td>0.411469</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:06:00</th>\n",
       "      <td>0.338686</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:07:00</th>\n",
       "      <td>0.612961</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:08:00</th>\n",
       "      <td>0.930348</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:09:00</th>\n",
       "      <td>-1.145872</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:10:00</th>\n",
       "      <td>1.824010</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:11:00</th>\n",
       "      <td>-1.901515</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:12:00</th>\n",
       "      <td>-1.906472</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:13:00</th>\n",
       "      <td>-1.342346</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:14:00</th>\n",
       "      <td>-0.565809</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:15:00</th>\n",
       "      <td>-0.611475</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:16:00</th>\n",
       "      <td>1.001233</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:17:00</th>\n",
       "      <td>0.795559</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:18:00</th>\n",
       "      <td>0.364509</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:19:00</th>\n",
       "      <td>-0.761569</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:20:00</th>\n",
       "      <td>1.388499</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:21:00</th>\n",
       "      <td>2.271801</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:22:00</th>\n",
       "      <td>0.559616</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:23:00</th>\n",
       "      <td>0.559026</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:24:00</th>\n",
       "      <td>-0.434898</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:25:00</th>\n",
       "      <td>-1.192385</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:26:00</th>\n",
       "      <td>0.255456</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:27:00</th>\n",
       "      <td>-0.661277</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:28:00</th>\n",
       "      <td>0.152875</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:29:00</th>\n",
       "      <td>0.326932</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-02-27 23:31:00</th>\n",
       "      <td>0.336765</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-02-27 23:32:00</th>\n",
       "      <td>-0.081882</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-02-27 23:33:00</th>\n",
       "      <td>-1.429592</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-02-27 23:34:00</th>\n",
       "      <td>-0.183730</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-02-27 23:35:00</th>\n",
       "      <td>0.273969</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-02-27 23:36:00</th>\n",
       "      <td>-0.199049</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-02-27 23:37:00</th>\n",
       "      <td>-1.014975</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-02-27 23:38:00</th>\n",
       "      <td>1.578384</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-02-27 23:39:00</th>\n",
       "      <td>-0.767711</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-02-27 23:40:00</th>\n",
       "      <td>0.132272</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-02-27 23:41:00</th>\n",
       "      <td>-0.585182</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-02-27 23:42:00</th>\n",
       "      <td>-0.113854</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-02-27 23:43:00</th>\n",
       "      <td>-0.404606</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-02-27 23:44:00</th>\n",
       "      <td>2.073720</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-02-27 23:45:00</th>\n",
       "      <td>-0.857804</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-02-27 23:46:00</th>\n",
       "      <td>0.302374</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-02-27 23:47:00</th>\n",
       "      <td>-1.378492</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-02-27 23:48:00</th>\n",
       "      <td>-1.447382</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-02-27 23:49:00</th>\n",
       "      <td>0.803564</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-02-27 23:50:00</th>\n",
       "      <td>-1.603786</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-02-27 23:51:00</th>\n",
       "      <td>-0.199526</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-02-27 23:52:00</th>\n",
       "      <td>0.102468</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-02-27 23:53:00</th>\n",
       "      <td>-2.546855</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-02-27 23:54:00</th>\n",
       "      <td>0.687578</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-02-27 23:55:00</th>\n",
       "      <td>1.185959</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-02-27 23:56:00</th>\n",
       "      <td>-0.403511</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-02-27 23:57:00</th>\n",
       "      <td>0.506532</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-02-27 23:58:00</th>\n",
       "      <td>0.721674</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-02-27 23:59:00</th>\n",
       "      <td>0.055642</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-02-28 00:00:00</th>\n",
       "      <td>1.351408</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>83521 rows × 1 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                            A\n",
       "2013-01-01 00:00:00  0.237964\n",
       "2013-01-01 00:01:00  0.227762\n",
       "2013-01-01 00:02:00 -0.041934\n",
       "2013-01-01 00:03:00  0.623022\n",
       "2013-01-01 00:04:00 -0.520355\n",
       "2013-01-01 00:05:00  0.411469\n",
       "2013-01-01 00:06:00  0.338686\n",
       "2013-01-01 00:07:00  0.612961\n",
       "2013-01-01 00:08:00  0.930348\n",
       "2013-01-01 00:09:00 -1.145872\n",
       "2013-01-01 00:10:00  1.824010\n",
       "2013-01-01 00:11:00 -1.901515\n",
       "2013-01-01 00:12:00 -1.906472\n",
       "2013-01-01 00:13:00 -1.342346\n",
       "2013-01-01 00:14:00 -0.565809\n",
       "2013-01-01 00:15:00 -0.611475\n",
       "2013-01-01 00:16:00  1.001233\n",
       "2013-01-01 00:17:00  0.795559\n",
       "2013-01-01 00:18:00  0.364509\n",
       "2013-01-01 00:19:00 -0.761569\n",
       "2013-01-01 00:20:00  1.388499\n",
       "2013-01-01 00:21:00  2.271801\n",
       "2013-01-01 00:22:00  0.559616\n",
       "2013-01-01 00:23:00  0.559026\n",
       "2013-01-01 00:24:00 -0.434898\n",
       "2013-01-01 00:25:00 -1.192385\n",
       "2013-01-01 00:26:00  0.255456\n",
       "2013-01-01 00:27:00 -0.661277\n",
       "2013-01-01 00:28:00  0.152875\n",
       "2013-01-01 00:29:00  0.326932\n",
       "...                       ...\n",
       "2013-02-27 23:31:00  0.336765\n",
       "2013-02-27 23:32:00 -0.081882\n",
       "2013-02-27 23:33:00 -1.429592\n",
       "2013-02-27 23:34:00 -0.183730\n",
       "2013-02-27 23:35:00  0.273969\n",
       "2013-02-27 23:36:00 -0.199049\n",
       "2013-02-27 23:37:00 -1.014975\n",
       "2013-02-27 23:38:00  1.578384\n",
       "2013-02-27 23:39:00 -0.767711\n",
       "2013-02-27 23:40:00  0.132272\n",
       "2013-02-27 23:41:00 -0.585182\n",
       "2013-02-27 23:42:00 -0.113854\n",
       "2013-02-27 23:43:00 -0.404606\n",
       "2013-02-27 23:44:00  2.073720\n",
       "2013-02-27 23:45:00 -0.857804\n",
       "2013-02-27 23:46:00  0.302374\n",
       "2013-02-27 23:47:00 -1.378492\n",
       "2013-02-27 23:48:00 -1.447382\n",
       "2013-02-27 23:49:00  0.803564\n",
       "2013-02-27 23:50:00 -1.603786\n",
       "2013-02-27 23:51:00 -0.199526\n",
       "2013-02-27 23:52:00  0.102468\n",
       "2013-02-27 23:53:00 -2.546855\n",
       "2013-02-27 23:54:00  0.687578\n",
       "2013-02-27 23:55:00  1.185959\n",
       "2013-02-27 23:56:00 -0.403511\n",
       "2013-02-27 23:57:00  0.506532\n",
       "2013-02-27 23:58:00  0.721674\n",
       "2013-02-27 23:59:00  0.055642\n",
       "2013-02-28 00:00:00  1.351408\n",
       "\n",
       "[83521 rows x 1 columns]"
      ]
     },
     "execution_count": 91,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dft['2013-1':'2013-2-28 00:00:00']"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We are stopping on the included end-point as it is part of the index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 92,
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2013-01-15 00:00:00</th>\n",
       "      <td>0.693073</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-15 00:01:00</th>\n",
       "      <td>0.803808</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-15 00:02:00</th>\n",
       "      <td>0.093832</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-15 00:03:00</th>\n",
       "      <td>-0.642350</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-15 00:04:00</th>\n",
       "      <td>0.656836</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-15 00:05:00</th>\n",
       "      <td>-0.087465</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-15 00:06:00</th>\n",
       "      <td>-0.445697</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-15 00:07:00</th>\n",
       "      <td>0.454253</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-15 00:08:00</th>\n",
       "      <td>1.412341</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-15 00:09:00</th>\n",
       "      <td>-1.205156</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-15 00:10:00</th>\n",
       "      <td>-0.494120</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-15 00:11:00</th>\n",
       "      <td>0.583195</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-15 00:12:00</th>\n",
       "      <td>-0.480749</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-15 00:13:00</th>\n",
       "      <td>0.557163</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-15 00:14:00</th>\n",
       "      <td>0.870796</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-15 00:15:00</th>\n",
       "      <td>-0.284227</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-15 00:16:00</th>\n",
       "      <td>0.146246</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-15 00:17:00</th>\n",
       "      <td>-1.149551</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-15 00:18:00</th>\n",
       "      <td>-0.274975</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-15 00:19:00</th>\n",
       "      <td>0.523071</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-15 00:20:00</th>\n",
       "      <td>-0.724992</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-15 00:21:00</th>\n",
       "      <td>-0.558874</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-15 00:22:00</th>\n",
       "      <td>0.799084</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-15 00:23:00</th>\n",
       "      <td>1.145274</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-15 00:24:00</th>\n",
       "      <td>-0.497442</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-15 00:25:00</th>\n",
       "      <td>0.295290</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-15 00:26:00</th>\n",
       "      <td>-1.145164</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-15 00:27:00</th>\n",
       "      <td>-1.121622</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-15 00:28:00</th>\n",
       "      <td>0.243357</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-15 00:29:00</th>\n",
       "      <td>-0.118923</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-15 12:01:00</th>\n",
       "      <td>-1.542250</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-15 12:02:00</th>\n",
       "      <td>-1.517605</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-15 12:03:00</th>\n",
       "      <td>0.442370</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-15 12:04:00</th>\n",
       "      <td>0.353482</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-15 12:05:00</th>\n",
       "      <td>-0.229453</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-15 12:06:00</th>\n",
       "      <td>0.515005</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-15 12:07:00</th>\n",
       "      <td>0.037835</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-15 12:08:00</th>\n",
       "      <td>1.997062</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-15 12:09:00</th>\n",
       "      <td>0.189187</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-15 12:10:00</th>\n",
       "      <td>1.593068</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-15 12:11:00</th>\n",
       "      <td>-0.003008</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-15 12:12:00</th>\n",
       "      <td>1.117998</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-15 12:13:00</th>\n",
       "      <td>-1.498224</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-15 12:14:00</th>\n",
       "      <td>-1.175020</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-15 12:15:00</th>\n",
       "      <td>0.692126</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-15 12:16:00</th>\n",
       "      <td>-0.011171</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-15 12:17:00</th>\n",
       "      <td>-1.395708</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-15 12:18:00</th>\n",
       "      <td>0.713964</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-15 12:19:00</th>\n",
       "      <td>-2.189412</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-15 12:20:00</th>\n",
       "      <td>1.436179</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-15 12:21:00</th>\n",
       "      <td>2.571658</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-15 12:22:00</th>\n",
       "      <td>0.122502</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-15 12:23:00</th>\n",
       "      <td>-0.808520</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-15 12:24:00</th>\n",
       "      <td>-0.354701</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-15 12:25:00</th>\n",
       "      <td>1.699730</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-15 12:26:00</th>\n",
       "      <td>1.723131</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-15 12:27:00</th>\n",
       "      <td>-0.823745</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-15 12:28:00</th>\n",
       "      <td>1.464019</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-15 12:29:00</th>\n",
       "      <td>0.909165</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-15 12:30:00</th>\n",
       "      <td>-0.916315</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>751 rows × 1 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                            A\n",
       "2013-01-15 00:00:00  0.693073\n",
       "2013-01-15 00:01:00  0.803808\n",
       "2013-01-15 00:02:00  0.093832\n",
       "2013-01-15 00:03:00 -0.642350\n",
       "2013-01-15 00:04:00  0.656836\n",
       "2013-01-15 00:05:00 -0.087465\n",
       "2013-01-15 00:06:00 -0.445697\n",
       "2013-01-15 00:07:00  0.454253\n",
       "2013-01-15 00:08:00  1.412341\n",
       "2013-01-15 00:09:00 -1.205156\n",
       "2013-01-15 00:10:00 -0.494120\n",
       "2013-01-15 00:11:00  0.583195\n",
       "2013-01-15 00:12:00 -0.480749\n",
       "2013-01-15 00:13:00  0.557163\n",
       "2013-01-15 00:14:00  0.870796\n",
       "2013-01-15 00:15:00 -0.284227\n",
       "2013-01-15 00:16:00  0.146246\n",
       "2013-01-15 00:17:00 -1.149551\n",
       "2013-01-15 00:18:00 -0.274975\n",
       "2013-01-15 00:19:00  0.523071\n",
       "2013-01-15 00:20:00 -0.724992\n",
       "2013-01-15 00:21:00 -0.558874\n",
       "2013-01-15 00:22:00  0.799084\n",
       "2013-01-15 00:23:00  1.145274\n",
       "2013-01-15 00:24:00 -0.497442\n",
       "2013-01-15 00:25:00  0.295290\n",
       "2013-01-15 00:26:00 -1.145164\n",
       "2013-01-15 00:27:00 -1.121622\n",
       "2013-01-15 00:28:00  0.243357\n",
       "2013-01-15 00:29:00 -0.118923\n",
       "...                       ...\n",
       "2013-01-15 12:01:00 -1.542250\n",
       "2013-01-15 12:02:00 -1.517605\n",
       "2013-01-15 12:03:00  0.442370\n",
       "2013-01-15 12:04:00  0.353482\n",
       "2013-01-15 12:05:00 -0.229453\n",
       "2013-01-15 12:06:00  0.515005\n",
       "2013-01-15 12:07:00  0.037835\n",
       "2013-01-15 12:08:00  1.997062\n",
       "2013-01-15 12:09:00  0.189187\n",
       "2013-01-15 12:10:00  1.593068\n",
       "2013-01-15 12:11:00 -0.003008\n",
       "2013-01-15 12:12:00  1.117998\n",
       "2013-01-15 12:13:00 -1.498224\n",
       "2013-01-15 12:14:00 -1.175020\n",
       "2013-01-15 12:15:00  0.692126\n",
       "2013-01-15 12:16:00 -0.011171\n",
       "2013-01-15 12:17:00 -1.395708\n",
       "2013-01-15 12:18:00  0.713964\n",
       "2013-01-15 12:19:00 -2.189412\n",
       "2013-01-15 12:20:00  1.436179\n",
       "2013-01-15 12:21:00  2.571658\n",
       "2013-01-15 12:22:00  0.122502\n",
       "2013-01-15 12:23:00 -0.808520\n",
       "2013-01-15 12:24:00 -0.354701\n",
       "2013-01-15 12:25:00  1.699730\n",
       "2013-01-15 12:26:00  1.723131\n",
       "2013-01-15 12:27:00 -0.823745\n",
       "2013-01-15 12:28:00  1.464019\n",
       "2013-01-15 12:29:00  0.909165\n",
       "2013-01-15 12:30:00 -0.916315\n",
       "\n",
       "[751 rows x 1 columns]"
      ]
     },
     "execution_count": 92,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dft['2013-1-15':'2013-1-15 12:30:00']"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "DatetimeIndex partial string indexing also works on a DataFrame with a MultiIndex:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 93,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">2013-01-01 00:00:00</th>\n",
       "      <th>a</th>\n",
       "      <td>0.487733</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>b</th>\n",
       "      <td>-0.527890</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">2013-01-01 12:00:00</th>\n",
       "      <th>a</th>\n",
       "      <td>-1.315374</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>b</th>\n",
       "      <td>0.970578</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">2013-01-02 00:00:00</th>\n",
       "      <th>a</th>\n",
       "      <td>0.535436</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>b</th>\n",
       "      <td>0.129342</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">2013-01-02 12:00:00</th>\n",
       "      <th>a</th>\n",
       "      <td>0.536116</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>b</th>\n",
       "      <td>1.402482</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">2013-01-03 00:00:00</th>\n",
       "      <th>a</th>\n",
       "      <td>-0.908913</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>b</th>\n",
       "      <td>-0.564002</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">2013-01-03 12:00:00</th>\n",
       "      <th>a</th>\n",
       "      <td>-0.634409</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>b</th>\n",
       "      <td>-1.583892</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">2013-01-04 00:00:00</th>\n",
       "      <th>a</th>\n",
       "      <td>-0.258303</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>b</th>\n",
       "      <td>-0.228866</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">2013-01-04 12:00:00</th>\n",
       "      <th>a</th>\n",
       "      <td>-0.596155</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>b</th>\n",
       "      <td>0.069771</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">2013-01-05 00:00:00</th>\n",
       "      <th>a</th>\n",
       "      <td>0.508221</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>b</th>\n",
       "      <td>0.405532</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">2013-01-05 12:00:00</th>\n",
       "      <th>a</th>\n",
       "      <td>0.603411</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>b</th>\n",
       "      <td>-0.924896</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                              A\n",
       "2013-01-01 00:00:00 a  0.487733\n",
       "                    b -0.527890\n",
       "2013-01-01 12:00:00 a -1.315374\n",
       "                    b  0.970578\n",
       "2013-01-02 00:00:00 a  0.535436\n",
       "                    b  0.129342\n",
       "2013-01-02 12:00:00 a  0.536116\n",
       "                    b  1.402482\n",
       "2013-01-03 00:00:00 a -0.908913\n",
       "                    b -0.564002\n",
       "2013-01-03 12:00:00 a -0.634409\n",
       "                    b -1.583892\n",
       "2013-01-04 00:00:00 a -0.258303\n",
       "                    b -0.228866\n",
       "2013-01-04 12:00:00 a -0.596155\n",
       "                    b  0.069771\n",
       "2013-01-05 00:00:00 a  0.508221\n",
       "                    b  0.405532\n",
       "2013-01-05 12:00:00 a  0.603411\n",
       "                    b -0.924896"
      ]
     },
     "execution_count": 93,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dft2 = pd.DataFrame(np.random.randn(20, 1),\n",
    "                    columns=['A'],\n",
    "                    index=pd.MultiIndex.from_product([pd.date_range('20130101',periods=10,freq='12H'),['a', 'b']]))\n",
    "dft2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 94,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">2013-01-05 00:00:00</th>\n",
       "      <th>a</th>\n",
       "      <td>0.508221</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>b</th>\n",
       "      <td>0.405532</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">2013-01-05 12:00:00</th>\n",
       "      <th>a</th>\n",
       "      <td>0.603411</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>b</th>\n",
       "      <td>-0.924896</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                              A\n",
       "2013-01-05 00:00:00 a  0.508221\n",
       "                    b  0.405532\n",
       "2013-01-05 12:00:00 a  0.603411\n",
       "                    b -0.924896"
      ]
     },
     "execution_count": 94,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dft2.loc['2013-01-05']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 95,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "idx = pd.IndexSlice"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 98,
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"10\" valign=\"top\">a</th>\n",
       "      <th>2013-01-01 00:00:00</th>\n",
       "      <td>0.487733</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 12:00:00</th>\n",
       "      <td>-1.315374</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-02 00:00:00</th>\n",
       "      <td>0.535436</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-02 12:00:00</th>\n",
       "      <td>0.536116</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-03 00:00:00</th>\n",
       "      <td>-0.908913</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-03 12:00:00</th>\n",
       "      <td>-0.634409</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-04 00:00:00</th>\n",
       "      <td>-0.258303</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-04 12:00:00</th>\n",
       "      <td>-0.596155</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-05 00:00:00</th>\n",
       "      <td>0.508221</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-05 12:00:00</th>\n",
       "      <td>0.603411</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"10\" valign=\"top\">b</th>\n",
       "      <th>2013-01-01 00:00:00</th>\n",
       "      <td>-0.527890</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 12:00:00</th>\n",
       "      <td>0.970578</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-02 00:00:00</th>\n",
       "      <td>0.129342</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-02 12:00:00</th>\n",
       "      <td>1.402482</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-03 00:00:00</th>\n",
       "      <td>-0.564002</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-03 12:00:00</th>\n",
       "      <td>-1.583892</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-04 00:00:00</th>\n",
       "      <td>-0.228866</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-04 12:00:00</th>\n",
       "      <td>0.069771</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-05 00:00:00</th>\n",
       "      <td>0.405532</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-05 12:00:00</th>\n",
       "      <td>-0.924896</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                              A\n",
       "a 2013-01-01 00:00:00  0.487733\n",
       "  2013-01-01 12:00:00 -1.315374\n",
       "  2013-01-02 00:00:00  0.535436\n",
       "  2013-01-02 12:00:00  0.536116\n",
       "  2013-01-03 00:00:00 -0.908913\n",
       "  2013-01-03 12:00:00 -0.634409\n",
       "  2013-01-04 00:00:00 -0.258303\n",
       "  2013-01-04 12:00:00 -0.596155\n",
       "  2013-01-05 00:00:00  0.508221\n",
       "  2013-01-05 12:00:00  0.603411\n",
       "b 2013-01-01 00:00:00 -0.527890\n",
       "  2013-01-01 12:00:00  0.970578\n",
       "  2013-01-02 00:00:00  0.129342\n",
       "  2013-01-02 12:00:00  1.402482\n",
       "  2013-01-03 00:00:00 -0.564002\n",
       "  2013-01-03 12:00:00 -1.583892\n",
       "  2013-01-04 00:00:00 -0.228866\n",
       "  2013-01-04 12:00:00  0.069771\n",
       "  2013-01-05 00:00:00  0.405532\n",
       "  2013-01-05 12:00:00 -0.924896"
      ]
     },
     "execution_count": 98,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dft2 = dft2.swaplevel(0, 1).sort_index()\n",
    "dft2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 99,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">a</th>\n",
       "      <th>2013-01-05 00:00:00</th>\n",
       "      <td>0.508221</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-05 12:00:00</th>\n",
       "      <td>0.603411</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">b</th>\n",
       "      <th>2013-01-05 00:00:00</th>\n",
       "      <td>0.405532</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-05 12:00:00</th>\n",
       "      <td>-0.924896</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                              A\n",
       "a 2013-01-05 00:00:00  0.508221\n",
       "  2013-01-05 12:00:00  0.603411\n",
       "b 2013-01-05 00:00:00  0.405532\n",
       "  2013-01-05 12:00:00 -0.924896"
      ]
     },
     "execution_count": 99,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dft2.loc[idx[:, '2013-01-05'], :]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 19.6.2 Slice vs. Exact Match"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The same string used as an indexing parameter can be treated either as a slice or as an exact match depending on the\n",
    "resolution of the index. If the string is less accurate than the index, it will be treated as a slice, otherwise as an exact\n",
    "match.\n",
    "\n",
    "需要注意的是Slice和Exact match输出的结果是类型是不同的。\n",
    "\n",
    "resolution的意思是精度，分辨率\n",
    "\n",
    "Consider a Series object with a minute resolution index:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'minute'"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "series_minute = pd.Series([1, 2, 3],\n",
    "                          pd.DatetimeIndex(['2011-12-31 23:59:00',\n",
    "                                            '2012-01-01 00:00:00',\n",
    "                                            '2012-01-01 00:02:00']))\n",
    "series_minute.index.resolution"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "A timestamp string less accurate than a minute gives a Series object."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2011-12-31 23:59:00    1\n",
       "dtype: int64"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "series_minute['2011-12-31 23']"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "A timestamp string with minute resolution (or more accurate), gives a scalar instead, i.e. it is not casted to a slice."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "series_minute['2011-12-31 23:59']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "series_minute['2011-12-31 23:59:00']"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "If index resolution is second, then, the minute-accurate timestamp gives a Series."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'second'"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "series_second = pd.Series([1, 2, 3],pd.DatetimeIndex(['2011-12-31 23:59:59',\n",
    "                                                      '2012-01-01 00:00:00',\n",
    "                                                      '2012-01-01 00:00:01']))\n",
    "series_second.index.resolution"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2011-12-31 23:59:59    1\n",
       "dtype: int64"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "series_second['2011-12-31 23:59']"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "If the timestamp string is treated as a slice, it can be used to index DataFrame with [] as well."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>a</th>\n",
       "      <th>b</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2011-12-31 23:59:00</th>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2012-01-01 00:00:00</th>\n",
       "      <td>2</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2012-01-01 00:02:00</th>\n",
       "      <td>3</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                     a  b\n",
       "2011-12-31 23:59:00  1  4\n",
       "2012-01-01 00:00:00  2  5\n",
       "2012-01-01 00:02:00  3  6"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dft_minute = pd.DataFrame({'a': [1, 2, 3], 'b': [4, 5, 6]},index=series_minute.index)\n",
    "dft_minute"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>a</th>\n",
       "      <th>b</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2011-12-31 23:59:00</th>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                     a  b\n",
       "2011-12-31 23:59:00  1  4"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dft_minute['2011-12-31 23']"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "However, if the string is treated as an exact match, the selection in DataFrame‘s [] will be column-\n",
    "wise and not row-wise, see Indexing Basics. For example dft_minute['2011-12-31 23:59'] will raise\n",
    "KeyError as '2012-12-31 23:59' has the same resolution as the index and there is no column with such\n",
    "name:\n",
    "To always have unambiguous selection, whether the row is treated as a slice or a single selection, use .loc."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Note also that DatetimeIndex resolution cannot be less precise than day."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2011-12-01    1\n",
       "2012-01-01    2\n",
       "2012-02-01    3\n",
       "dtype: int64"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "series_monthly = pd.Series([1, 2, 3],\n",
    "                           pd.DatetimeIndex(['2011-12',\n",
    "                                             '2012-01',\n",
    "                                             '2012-02']))\n",
    "series_monthly"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'day'"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "series_monthly.index.resolution"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2011-12-01    1\n",
       "dtype: int64"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "series_monthly['2011-12'] # returns Series"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 19.6.3 Exact Indexing"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:00:00</th>\n",
       "      <td>0.159180</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:01:00</th>\n",
       "      <td>-0.371462</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:02:00</th>\n",
       "      <td>0.454213</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:03:00</th>\n",
       "      <td>0.711532</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:04:00</th>\n",
       "      <td>-0.223610</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:05:00</th>\n",
       "      <td>0.834068</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:06:00</th>\n",
       "      <td>-0.048895</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:07:00</th>\n",
       "      <td>-0.591174</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:08:00</th>\n",
       "      <td>-1.535174</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:09:00</th>\n",
       "      <td>1.178111</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:10:00</th>\n",
       "      <td>-0.775582</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:11:00</th>\n",
       "      <td>-0.210906</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:12:00</th>\n",
       "      <td>0.766672</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:13:00</th>\n",
       "      <td>0.933659</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:14:00</th>\n",
       "      <td>1.853951</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:15:00</th>\n",
       "      <td>0.740702</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:16:00</th>\n",
       "      <td>0.591871</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:17:00</th>\n",
       "      <td>1.736773</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:18:00</th>\n",
       "      <td>1.153951</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:19:00</th>\n",
       "      <td>1.982074</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:20:00</th>\n",
       "      <td>-0.294754</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:21:00</th>\n",
       "      <td>1.175221</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:22:00</th>\n",
       "      <td>-0.629549</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:23:00</th>\n",
       "      <td>0.917592</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:24:00</th>\n",
       "      <td>-1.562463</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:25:00</th>\n",
       "      <td>-0.123375</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:26:00</th>\n",
       "      <td>-0.740918</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:27:00</th>\n",
       "      <td>-1.033469</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:28:00</th>\n",
       "      <td>0.060992</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 00:29:00</th>\n",
       "      <td>-0.862869</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-02-27 23:31:00</th>\n",
       "      <td>-0.407629</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-02-27 23:32:00</th>\n",
       "      <td>-0.105581</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-02-27 23:33:00</th>\n",
       "      <td>0.643671</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-02-27 23:34:00</th>\n",
       "      <td>-0.137034</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-02-27 23:35:00</th>\n",
       "      <td>-0.417109</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-02-27 23:36:00</th>\n",
       "      <td>-1.289510</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-02-27 23:37:00</th>\n",
       "      <td>0.344209</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-02-27 23:38:00</th>\n",
       "      <td>0.878875</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-02-27 23:39:00</th>\n",
       "      <td>0.405162</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-02-27 23:40:00</th>\n",
       "      <td>0.482947</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-02-27 23:41:00</th>\n",
       "      <td>-0.425264</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-02-27 23:42:00</th>\n",
       "      <td>-0.245031</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-02-27 23:43:00</th>\n",
       "      <td>0.008033</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-02-27 23:44:00</th>\n",
       "      <td>0.296386</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-02-27 23:45:00</th>\n",
       "      <td>-1.686124</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-02-27 23:46:00</th>\n",
       "      <td>-1.091266</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-02-27 23:47:00</th>\n",
       "      <td>-1.857988</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-02-27 23:48:00</th>\n",
       "      <td>-1.273468</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-02-27 23:49:00</th>\n",
       "      <td>1.118216</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-02-27 23:50:00</th>\n",
       "      <td>1.001181</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-02-27 23:51:00</th>\n",
       "      <td>0.712514</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-02-27 23:52:00</th>\n",
       "      <td>0.473674</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-02-27 23:53:00</th>\n",
       "      <td>-0.212258</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-02-27 23:54:00</th>\n",
       "      <td>0.898936</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-02-27 23:55:00</th>\n",
       "      <td>-0.134722</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-02-27 23:56:00</th>\n",
       "      <td>-0.533463</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-02-27 23:57:00</th>\n",
       "      <td>-0.368767</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-02-27 23:58:00</th>\n",
       "      <td>-1.075970</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-02-27 23:59:00</th>\n",
       "      <td>-0.888992</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-02-28 00:00:00</th>\n",
       "      <td>0.745469</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>83521 rows × 1 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                            A\n",
       "2013-01-01 00:00:00  0.159180\n",
       "2013-01-01 00:01:00 -0.371462\n",
       "2013-01-01 00:02:00  0.454213\n",
       "2013-01-01 00:03:00  0.711532\n",
       "2013-01-01 00:04:00 -0.223610\n",
       "2013-01-01 00:05:00  0.834068\n",
       "2013-01-01 00:06:00 -0.048895\n",
       "2013-01-01 00:07:00 -0.591174\n",
       "2013-01-01 00:08:00 -1.535174\n",
       "2013-01-01 00:09:00  1.178111\n",
       "2013-01-01 00:10:00 -0.775582\n",
       "2013-01-01 00:11:00 -0.210906\n",
       "2013-01-01 00:12:00  0.766672\n",
       "2013-01-01 00:13:00  0.933659\n",
       "2013-01-01 00:14:00  1.853951\n",
       "2013-01-01 00:15:00  0.740702\n",
       "2013-01-01 00:16:00  0.591871\n",
       "2013-01-01 00:17:00  1.736773\n",
       "2013-01-01 00:18:00  1.153951\n",
       "2013-01-01 00:19:00  1.982074\n",
       "2013-01-01 00:20:00 -0.294754\n",
       "2013-01-01 00:21:00  1.175221\n",
       "2013-01-01 00:22:00 -0.629549\n",
       "2013-01-01 00:23:00  0.917592\n",
       "2013-01-01 00:24:00 -1.562463\n",
       "2013-01-01 00:25:00 -0.123375\n",
       "2013-01-01 00:26:00 -0.740918\n",
       "2013-01-01 00:27:00 -1.033469\n",
       "2013-01-01 00:28:00  0.060992\n",
       "2013-01-01 00:29:00 -0.862869\n",
       "...                       ...\n",
       "2013-02-27 23:31:00 -0.407629\n",
       "2013-02-27 23:32:00 -0.105581\n",
       "2013-02-27 23:33:00  0.643671\n",
       "2013-02-27 23:34:00 -0.137034\n",
       "2013-02-27 23:35:00 -0.417109\n",
       "2013-02-27 23:36:00 -1.289510\n",
       "2013-02-27 23:37:00  0.344209\n",
       "2013-02-27 23:38:00  0.878875\n",
       "2013-02-27 23:39:00  0.405162\n",
       "2013-02-27 23:40:00  0.482947\n",
       "2013-02-27 23:41:00 -0.425264\n",
       "2013-02-27 23:42:00 -0.245031\n",
       "2013-02-27 23:43:00  0.008033\n",
       "2013-02-27 23:44:00  0.296386\n",
       "2013-02-27 23:45:00 -1.686124\n",
       "2013-02-27 23:46:00 -1.091266\n",
       "2013-02-27 23:47:00 -1.857988\n",
       "2013-02-27 23:48:00 -1.273468\n",
       "2013-02-27 23:49:00  1.118216\n",
       "2013-02-27 23:50:00  1.001181\n",
       "2013-02-27 23:51:00  0.712514\n",
       "2013-02-27 23:52:00  0.473674\n",
       "2013-02-27 23:53:00 -0.212258\n",
       "2013-02-27 23:54:00  0.898936\n",
       "2013-02-27 23:55:00 -0.134722\n",
       "2013-02-27 23:56:00 -0.533463\n",
       "2013-02-27 23:57:00 -0.368767\n",
       "2013-02-27 23:58:00 -1.075970\n",
       "2013-02-27 23:59:00 -0.888992\n",
       "2013-02-28 00:00:00  0.745469\n",
       "\n",
       "[83521 rows x 1 columns]"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dft[datetime(2013, 1, 1):datetime(2013,2,28)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "collapsed": false,
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2013-01-01 10:12:00</th>\n",
       "      <td>1.693907</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 10:13:00</th>\n",
       "      <td>1.418449</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 10:14:00</th>\n",
       "      <td>-0.597315</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 10:15:00</th>\n",
       "      <td>-0.333619</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 10:16:00</th>\n",
       "      <td>0.826883</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 10:17:00</th>\n",
       "      <td>0.695949</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 10:18:00</th>\n",
       "      <td>0.086018</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 10:19:00</th>\n",
       "      <td>0.773490</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 10:20:00</th>\n",
       "      <td>-0.304660</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 10:21:00</th>\n",
       "      <td>-1.396825</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 10:22:00</th>\n",
       "      <td>-0.438060</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 10:23:00</th>\n",
       "      <td>1.357287</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 10:24:00</th>\n",
       "      <td>-0.297679</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 10:25:00</th>\n",
       "      <td>-1.921801</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 10:26:00</th>\n",
       "      <td>-0.431413</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 10:27:00</th>\n",
       "      <td>-1.107795</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 10:28:00</th>\n",
       "      <td>0.737971</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 10:29:00</th>\n",
       "      <td>0.379352</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 10:30:00</th>\n",
       "      <td>-0.869431</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 10:31:00</th>\n",
       "      <td>-0.473302</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 10:32:00</th>\n",
       "      <td>-0.885839</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 10:33:00</th>\n",
       "      <td>2.132670</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 10:34:00</th>\n",
       "      <td>-0.842873</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 10:35:00</th>\n",
       "      <td>2.320734</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 10:36:00</th>\n",
       "      <td>0.936439</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 10:37:00</th>\n",
       "      <td>-2.358664</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 10:38:00</th>\n",
       "      <td>-1.689066</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 10:39:00</th>\n",
       "      <td>1.303019</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 10:40:00</th>\n",
       "      <td>-0.441863</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-01-01 10:41:00</th>\n",
       "      <td>-0.106134</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-02-28 09:43:00</th>\n",
       "      <td>0.142805</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-02-28 09:44:00</th>\n",
       "      <td>-2.233103</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-02-28 09:45:00</th>\n",
       "      <td>1.777913</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-02-28 09:46:00</th>\n",
       "      <td>0.234293</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-02-28 09:47:00</th>\n",
       "      <td>-0.042728</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-02-28 09:48:00</th>\n",
       "      <td>-1.242071</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-02-28 09:49:00</th>\n",
       "      <td>0.054276</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-02-28 09:50:00</th>\n",
       "      <td>1.302491</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-02-28 09:51:00</th>\n",
       "      <td>-1.118702</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-02-28 09:52:00</th>\n",
       "      <td>-0.349313</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-02-28 09:53:00</th>\n",
       "      <td>0.621645</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-02-28 09:54:00</th>\n",
       "      <td>-0.997399</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-02-28 09:55:00</th>\n",
       "      <td>1.213016</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-02-28 09:56:00</th>\n",
       "      <td>-0.197531</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-02-28 09:57:00</th>\n",
       "      <td>1.392967</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-02-28 09:58:00</th>\n",
       "      <td>-0.788952</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-02-28 09:59:00</th>\n",
       "      <td>-0.593744</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-02-28 10:00:00</th>\n",
       "      <td>0.414045</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-02-28 10:01:00</th>\n",
       "      <td>-1.032852</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-02-28 10:02:00</th>\n",
       "      <td>-1.221288</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-02-28 10:03:00</th>\n",
       "      <td>2.125129</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-02-28 10:04:00</th>\n",
       "      <td>0.322446</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-02-28 10:05:00</th>\n",
       "      <td>0.337315</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-02-28 10:06:00</th>\n",
       "      <td>-0.871711</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-02-28 10:07:00</th>\n",
       "      <td>-1.372183</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-02-28 10:08:00</th>\n",
       "      <td>0.492566</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-02-28 10:09:00</th>\n",
       "      <td>0.871924</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-02-28 10:10:00</th>\n",
       "      <td>0.567150</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-02-28 10:11:00</th>\n",
       "      <td>-0.559030</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-02-28 10:12:00</th>\n",
       "      <td>1.059373</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>83521 rows × 1 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                            A\n",
       "2013-01-01 10:12:00  1.693907\n",
       "2013-01-01 10:13:00  1.418449\n",
       "2013-01-01 10:14:00 -0.597315\n",
       "2013-01-01 10:15:00 -0.333619\n",
       "2013-01-01 10:16:00  0.826883\n",
       "2013-01-01 10:17:00  0.695949\n",
       "2013-01-01 10:18:00  0.086018\n",
       "2013-01-01 10:19:00  0.773490\n",
       "2013-01-01 10:20:00 -0.304660\n",
       "2013-01-01 10:21:00 -1.396825\n",
       "2013-01-01 10:22:00 -0.438060\n",
       "2013-01-01 10:23:00  1.357287\n",
       "2013-01-01 10:24:00 -0.297679\n",
       "2013-01-01 10:25:00 -1.921801\n",
       "2013-01-01 10:26:00 -0.431413\n",
       "2013-01-01 10:27:00 -1.107795\n",
       "2013-01-01 10:28:00  0.737971\n",
       "2013-01-01 10:29:00  0.379352\n",
       "2013-01-01 10:30:00 -0.869431\n",
       "2013-01-01 10:31:00 -0.473302\n",
       "2013-01-01 10:32:00 -0.885839\n",
       "2013-01-01 10:33:00  2.132670\n",
       "2013-01-01 10:34:00 -0.842873\n",
       "2013-01-01 10:35:00  2.320734\n",
       "2013-01-01 10:36:00  0.936439\n",
       "2013-01-01 10:37:00 -2.358664\n",
       "2013-01-01 10:38:00 -1.689066\n",
       "2013-01-01 10:39:00  1.303019\n",
       "2013-01-01 10:40:00 -0.441863\n",
       "2013-01-01 10:41:00 -0.106134\n",
       "...                       ...\n",
       "2013-02-28 09:43:00  0.142805\n",
       "2013-02-28 09:44:00 -2.233103\n",
       "2013-02-28 09:45:00  1.777913\n",
       "2013-02-28 09:46:00  0.234293\n",
       "2013-02-28 09:47:00 -0.042728\n",
       "2013-02-28 09:48:00 -1.242071\n",
       "2013-02-28 09:49:00  0.054276\n",
       "2013-02-28 09:50:00  1.302491\n",
       "2013-02-28 09:51:00 -1.118702\n",
       "2013-02-28 09:52:00 -0.349313\n",
       "2013-02-28 09:53:00  0.621645\n",
       "2013-02-28 09:54:00 -0.997399\n",
       "2013-02-28 09:55:00  1.213016\n",
       "2013-02-28 09:56:00 -0.197531\n",
       "2013-02-28 09:57:00  1.392967\n",
       "2013-02-28 09:58:00 -0.788952\n",
       "2013-02-28 09:59:00 -0.593744\n",
       "2013-02-28 10:00:00  0.414045\n",
       "2013-02-28 10:01:00 -1.032852\n",
       "2013-02-28 10:02:00 -1.221288\n",
       "2013-02-28 10:03:00  2.125129\n",
       "2013-02-28 10:04:00  0.322446\n",
       "2013-02-28 10:05:00  0.337315\n",
       "2013-02-28 10:06:00 -0.871711\n",
       "2013-02-28 10:07:00 -1.372183\n",
       "2013-02-28 10:08:00  0.492566\n",
       "2013-02-28 10:09:00  0.871924\n",
       "2013-02-28 10:10:00  0.567150\n",
       "2013-02-28 10:11:00 -0.559030\n",
       "2013-02-28 10:12:00  1.059373\n",
       "\n",
       "[83521 rows x 1 columns]"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dft[datetime(2013, 1, 1, 10, 12, 0):datetime(2013, 2, 28, 10, 12, 0)]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 19.6.4 Truncating & Fancy Indexing"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "rng2 = pd.date_range('2011-01-01', '2012-01-01', freq='W')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2011-01-02   -0.803905\n",
       "2011-01-09   -2.124541\n",
       "2011-01-16    1.458088\n",
       "2011-01-23   -1.306865\n",
       "2011-01-30   -1.364276\n",
       "2011-02-06   -0.093272\n",
       "2011-02-13   -0.823296\n",
       "2011-02-20   -0.557736\n",
       "2011-02-27    1.249951\n",
       "2011-03-06    0.119558\n",
       "2011-03-13    1.261920\n",
       "2011-03-20    0.550737\n",
       "2011-03-27    1.302723\n",
       "2011-04-03   -0.041571\n",
       "2011-04-10    0.631771\n",
       "2011-04-17   -1.555556\n",
       "2011-04-24   -0.234355\n",
       "2011-05-01   -0.087556\n",
       "2011-05-08    1.976625\n",
       "2011-05-15   -1.380098\n",
       "2011-05-22    1.425991\n",
       "2011-05-29   -1.164648\n",
       "2011-06-05    0.836807\n",
       "2011-06-12    1.714847\n",
       "2011-06-19    0.515634\n",
       "2011-06-26    0.544839\n",
       "2011-07-03   -0.239120\n",
       "2011-07-10    0.366387\n",
       "2011-07-17    0.362427\n",
       "2011-07-24   -0.160486\n",
       "2011-07-31   -1.752952\n",
       "2011-08-07   -0.467515\n",
       "2011-08-14   -0.632042\n",
       "2011-08-21   -0.118466\n",
       "2011-08-28   -1.644675\n",
       "2011-09-04    2.541993\n",
       "2011-09-11    0.197548\n",
       "2011-09-18   -0.487979\n",
       "2011-09-25    0.016660\n",
       "2011-10-02    0.234048\n",
       "2011-10-09   -1.044379\n",
       "2011-10-16   -0.608678\n",
       "2011-10-23   -0.319516\n",
       "2011-10-30   -0.655674\n",
       "2011-11-06   -1.828598\n",
       "2011-11-13   -0.917292\n",
       "2011-11-20   -0.171650\n",
       "2011-11-27    0.547767\n",
       "2011-12-04    0.345297\n",
       "2011-12-11   -0.030126\n",
       "2011-12-18    1.337480\n",
       "2011-12-25   -0.777997\n",
       "2012-01-01   -1.829705\n",
       "Freq: W-SUN, dtype: float64"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ts2 = pd.Series(np.random.randn(len(rng2)), index=rng2)\n",
    "ts2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2011-11-06   -1.828598\n",
       "2011-11-13   -0.917292\n",
       "2011-11-20   -0.171650\n",
       "2011-11-27    0.547767\n",
       "Freq: W-SUN, dtype: float64"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ts2.truncate(before='2011-11', after='2011-12')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2011-11-06   -1.828598\n",
       "2011-11-13   -0.917292\n",
       "2011-11-20   -0.171650\n",
       "2011-11-27    0.547767\n",
       "2011-12-04    0.345297\n",
       "2011-12-11   -0.030126\n",
       "2011-12-18    1.337480\n",
       "2011-12-25   -0.777997\n",
       "Freq: W-SUN, dtype: float64"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ts2['2011-11':'2011-12']"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 19.7 Time/Date Components"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 19.8 DateOffset Objects"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In the preceding examples, we created DatetimeIndex objects at various frequencies by passing in frequency\n",
    "strings like ‘M’, ‘W’, and ‘BM to the freq keyword. Under the hood, these frequency strings are being translated\n",
    "into an instance of pandas DateOffset, which represents a regular frequency increment. Specific offset logic like\n",
    "“month”, “business day”, or “one hour” is represented in its various subclasses."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The basic DateOffset takes the same arguments as dateutil.relativedelta, which works like:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'relativedelta' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-22-f5312d6436d0>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m      1\u001b[0m \u001b[0md\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mdatetime\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m2008\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m8\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m18\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m9\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m0\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0md\u001b[0m \u001b[1;33m+\u001b[0m \u001b[0mrelativedelta\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mmonths\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;36m4\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mdays\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;36m5\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;31mNameError\u001b[0m: name 'relativedelta' is not defined"
     ]
    }
   ],
   "source": [
    "d = datetime(2008, 8, 18, 9, 0)\n",
    "d + relativedelta(months=4, days=5)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We could have done the same thing with DateOffset:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 147,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from pandas.tseries.offsets import *"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Timestamp('2008-12-23 09:00:00')"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d + DateOffset(months=4, days=5)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The key features of a DateOffset object are:\n",
    "- it can be added / subtracted to/from a datetime object to obtain a shifted date\n",
    "- it can be multiplied by an integer (positive or negative) so that the increment will be applied multiple times\n",
    "- it has rollforward and rollback methods for moving a date forward or backward to the next or previous“offset date”"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Timestamp('2008-08-11 09:00:00')"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d - 5 * BDay()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Timestamp('2008-08-29 09:00:00')"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d + BMonthEnd()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "datetime.datetime(2008, 8, 18, 9, 0)"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "offset = BMonthEnd()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Timestamp('2008-08-29 09:00:00')"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "offset.rollforward(d)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Timestamp('2008-07-31 09:00:00')"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "offset.rollback(d)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "It’s definitely worth exploring the pandas.tseries.offsets module and the various docstrings for the classes.These operations (apply, rollforward and rollback) preserves time (hour, minute, etc) information by de-\n",
    "fault. To reset time, use normalize=True keyword when creating the offset instance. If normalize=True,result is normalized after the function is applied."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Timestamp('2014-01-02 09:00:00')"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "day = Day()\n",
    "day.apply(pd.Timestamp('2014-01-01 09:00'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Timestamp('2014-01-02 00:00:00')"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "day = Day(normalize=True)\n",
    "day.apply(pd.Timestamp('2014-01-01 09:00'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Timestamp('2014-01-01 23:00:00')"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "hour = Hour()\n",
    "hour.apply(pd.Timestamp('2014-01-01 22:00'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Timestamp('2014-01-01 00:00:00')"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "hour = Hour(normalize=True)\n",
    "hour.apply(pd.Timestamp('2014-01-01 22:00'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Timestamp('2014-01-02 00:00:00')"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "hour.apply(pd.Timestamp('2014-01-01 23:00'))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 19.8.1 Parametric Offsets"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "datetime.datetime(2008, 8, 18, 9, 0)"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Timestamp('2008-08-25 09:00:00')"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d + Week()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Timestamp('2008-08-22 09:00:00')"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d + Week(weekday=4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(d + Week(weekday=4)).weekday()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Timestamp('2008-08-11 09:00:00')"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d - Week()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Timestamp('2008-08-25 00:00:00')"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d + Week(normalize=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Timestamp('2008-08-11 00:00:00')"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d - Week(normalize=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Timestamp('2008-12-31 09:00:00')"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d + YearEnd()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Timestamp('2009-06-30 09:00:00')"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d + YearEnd(month=6)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 19.8.2 Using Offsets with Series / DatetimeIndex"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Offsets can be used with either a Series or DatetimeIndex to apply the offset to each element."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "DatetimeIndex(['2012-01-01', '2012-01-02', '2012-01-03'], dtype='datetime64[ns]', freq='D')"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rng = pd.date_range('2012-01-01', '2012-01-03')\n",
    "rng"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0   2012-01-01\n",
       "1   2012-01-02\n",
       "2   2012-01-03\n",
       "dtype: datetime64[ns]"
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s = pd.Series(rng)\n",
    "s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "DatetimeIndex(['2012-03-01', '2012-03-02', '2012-03-03'], dtype='datetime64[ns]', freq='D')"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rng + DateOffset(months=2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0   2012-03-01\n",
       "1   2012-03-02\n",
       "2   2012-03-03\n",
       "dtype: datetime64[ns]"
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s + DateOffset(months=2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0   2011-11-01\n",
       "1   2011-11-02\n",
       "2   2011-11-03\n",
       "dtype: datetime64[ns]"
      ]
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s - DateOffset(months=2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "If the offset class maps directly to a Timedelta (Day, Hour, Minute, Second, Micro, Milli, Nano) it can be\n",
    "used exactly like a Timedelta - see the Timedelta section for more examples."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0   2011-12-30\n",
       "1   2011-12-31\n",
       "2   2012-01-01\n",
       "dtype: datetime64[ns]"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s - Day(2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0   3 days\n",
       "1   3 days\n",
       "2   3 days\n",
       "dtype: timedelta64[ns]"
      ]
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "td = s - pd.Series(pd.date_range('2011-12-29', '2011-12-31'))\n",
    "td"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0   3 days 00:15:00\n",
       "1   3 days 00:15:00\n",
       "2   3 days 00:15:00\n",
       "dtype: timedelta64[ns]"
      ]
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "td + Minute(15)"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "Note that some offsets (such as BQuarterEnd) do not have a vectorized implementation. They can still be used but\n",
    "may calculate significantly slower and will show a PerformanceWarning"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "DatetimeIndex(['2012-03-30', '2012-03-30', '2012-03-30'], dtype='datetime64[ns]', freq='D')"
      ]
     },
     "execution_count": 62,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rng + BQuarterEnd()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 19.8.3 Custom Business Days"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The CDay or CustomBusinessDay class provides a parametric BusinessDay class which can be used to create customized business day calendars which account for local holidays and local weekend conventions.\n",
    "As an interesting example, let’s look at Egypt where a Friday-Saturday weekend is observed."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 84,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from pandas.tseries.offsets import CustomBusinessDay\n",
    "weekmask_egypt = 'Sun Mon Tue Wed Thu'\n",
    "holidays = ['2012-05-01', datetime(2013, 5, 1), np.datetime64('2014-05-01')]\n",
    "# 从calendar中剔除掉节假日以及周末就能够得到工作日\n",
    "bday_egypt = CustomBusinessDay(holidays=holidays, weekmask=weekmask_egypt)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Timestamp('2013-05-05 00:00:00')"
      ]
     },
     "execution_count": 66,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dt = datetime(2013, 4, 30)\n",
    "dt + 2*bday_egypt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "DatetimeIndex(['2013-04-30', '2013-05-02', '2013-05-05', '2013-05-06',\n",
       "               '2013-05-07'],\n",
       "              dtype='datetime64[ns]', freq='C')"
      ]
     },
     "execution_count": 72,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dts = pd.date_range(dt, periods=5, freq=bday_egypt)\n",
    "dts"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2013-04-30    1\n",
       "2013-05-02    3\n",
       "2013-05-05    6\n",
       "2013-05-06    0\n",
       "2013-05-07    1\n",
       "Freq: C, dtype: int64"
      ]
     },
     "execution_count": 74,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.Series(dts.weekday, dts)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    Mon\n",
       "1    Tue\n",
       "2    Wed\n",
       "3    Thu\n",
       "4    Fri\n",
       "5    Sat\n",
       "6    Sun\n",
       "dtype: object"
      ]
     },
     "execution_count": 75,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.Series('Mon Tue Wed Thu Fri Sat Sun'.split())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2013-04-30    Tue\n",
       "2013-05-02    Thu\n",
       "2013-05-05    Sun\n",
       "2013-05-06    Mon\n",
       "2013-05-07    Tue\n",
       "Freq: C, dtype: object"
      ]
     },
     "execution_count": 73,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.Series(dts.weekday, dts).map(pd.Series('Mon Tue Wed Thu Fri Sat Sun'.split()))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Holiday calendars can be used to provide the list of holidays. See the holiday calendar section for more information."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<CustomBusinessDay>"
      ]
     },
     "execution_count": 81,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from pandas.tseries.holiday import USFederalHolidayCalendar\n",
    "bday_us = CustomBusinessDay(calendar=USFederalHolidayCalendar())\n",
    "bday_us"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 86,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Timestamp('2014-01-21 00:00:00')"
      ]
     },
     "execution_count": 86,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Friday before MLK Day\n",
    "dt = datetime(2014, 1, 17)\n",
    "# Tuesday after MLK Day (Monday is skipped because it's a holiday) 下一个工作日\n",
    "dt + bday_us"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Monthly offsets that respect a certain holiday calendar can be defined in the usual way."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from pandas.tseries.offsets import CustomBusinessMonthBegin"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 88,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Timestamp('2014-01-02 00:00:00')"
      ]
     },
     "execution_count": 88,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#每个月的第一个工作日\n",
    "bmth_us = CustomBusinessMonthBegin(calendar=USFederalHolidayCalendar())\n",
    "# Skip new years\n",
    "dt = datetime(2013, 12, 17)\n",
    "dt + bmth_us"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 87,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "DatetimeIndex(['2010-01-04', '2010-02-01', '2010-03-01', '2010-04-01',\n",
       "               '2010-05-03', '2010-06-01', '2010-07-01', '2010-08-02',\n",
       "               '2010-09-01', '2010-10-01', '2010-11-01', '2010-12-01',\n",
       "               '2011-01-03', '2011-02-01', '2011-03-01', '2011-04-01',\n",
       "               '2011-05-02', '2011-06-01', '2011-07-01', '2011-08-01',\n",
       "               '2011-09-01', '2011-10-03', '2011-11-01', '2011-12-01'],\n",
       "              dtype='datetime64[ns]', freq='CBMS')"
      ]
     },
     "execution_count": 87,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Define date index with custom offset\n",
    "pd.DatetimeIndex(start='20100101',end='20120101',freq=bmth_us)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Note: The frequency string ‘C’ is used to indicate that a CustomBusinessDay DateOffset is used, it is important to note that since CustomBusinessDay is a parameterised type, instances of CustomBusinessDay may differ and this is\n",
    "not detectable from the ‘C’ frequency string. The user therefore needs to ensure that the ‘C’ frequency string is used consistently within the user’s application."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 19.8.4 Business Hour"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The BusinessHour class provides a business hour representation on BusinessDay, allowing to use specific start and end times."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "By default, BusinessHour uses 9:00 - 17:00 as business hours. Adding BusinessHour will increment Timestamp by hourly. If target Timestamp is out of business hours, move to the next business hour then increment it. If the result exceeds the business hours end, remaining is added to the next business day."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 91,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<BusinessHour: BH=09:00-17:00>"
      ]
     },
     "execution_count": 91,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "bh = BusinessHour()\n",
    "bh"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 92,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4"
      ]
     },
     "execution_count": 92,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 2014-08-01 is Friday\n",
    "pd.Timestamp('2014-08-01 10:00').weekday()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 93,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Timestamp('2014-08-01 11:00:00')"
      ]
     },
     "execution_count": 93,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.Timestamp('2014-08-01 10:00') + bh"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 94,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Timestamp('2014-08-01 10:00:00')"
      ]
     },
     "execution_count": 94,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Below example is the same as: pd.Timestamp('2014-08-01 09:00') + bh\n",
    "pd.Timestamp('2014-08-01 08:00') + bh"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 95,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Timestamp('2014-08-04 09:00:00')"
      ]
     },
     "execution_count": 95,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# If the results is on the end time, move to the next business day\n",
    "pd.Timestamp('2014-08-01 16:00') + bh"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 96,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Timestamp('2014-08-04 09:30:00')"
      ]
     },
     "execution_count": 96,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Remainings are added to the next day\n",
    "pd.Timestamp('2014-08-01 16:30') + bh"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 97,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Timestamp('2014-08-01 12:00:00')"
      ]
     },
     "execution_count": 97,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Adding 2 business hours\n",
    "pd.Timestamp('2014-08-01 10:00') + BusinessHour(2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 98,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Timestamp('2014-07-31 15:00:00')"
      ]
     },
     "execution_count": 98,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Subtracting 3 business hours\n",
    "pd.Timestamp('2014-08-01 10:00') + BusinessHour(-3)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Also, you can specify start and end time by keywords. Argument must be str which has hour:minute representation or datetime.time instance.Specifying seconds, microseconds and nanoseconds as business hour results in ValueError."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 99,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<BusinessHour: BH=11:00-20:00>"
      ]
     },
     "execution_count": 99,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "bh = BusinessHour(start='11:00', end=time(20, 0))\n",
    "bh"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 100,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Timestamp('2014-08-01 14:00:00')"
      ]
     },
     "execution_count": 100,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.Timestamp('2014-08-01 13:00') + bh"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 101,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Timestamp('2014-08-01 12:00:00')"
      ]
     },
     "execution_count": 101,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.Timestamp('2014-08-01 09:00') + bh"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 102,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Timestamp('2014-08-01 19:00:00')"
      ]
     },
     "execution_count": 102,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.Timestamp('2014-08-01 18:00') + bh"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Passing start time later than end represents midnight business hour. In this case, business hour exceeds midnight and overlap to the next day. Valid business hours are distinguished by whether it started from valid BusinessDay."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 103,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<BusinessHour: BH=17:00-09:00>"
      ]
     },
     "execution_count": 103,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "bh = BusinessHour(start='17:00', end='09:00')\n",
    "bh"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 104,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Timestamp('2014-08-01 18:00:00')"
      ]
     },
     "execution_count": 104,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.Timestamp('2014-08-01 17:00') + bh"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 105,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Timestamp('2014-08-02 00:00:00')"
      ]
     },
     "execution_count": 105,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.Timestamp('2014-08-01 23:00') + bh"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 107,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Timestamp('2014-08-02 05:00:00')"
      ]
     },
     "execution_count": 107,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Although 2014-08-02 is Satuaday,星期六\n",
    "# it is valid because it starts from 08-01 (Friday).\n",
    "pd.Timestamp('2014-08-02 04:00') + bh#还在工作日的工作时间区间内"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 108,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Timestamp('2014-08-04 18:00:00')"
      ]
     },
     "execution_count": 108,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Although 2014-08-04 is Monday,\n",
    "# it is out of business hours because it starts from 08-03 (Sunday).\n",
    "pd.Timestamp('2014-08-04 04:00') + bh"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 19.8.5 Custom Business Hour"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 19.8.6 Offset Aliases"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "|Alias|Description| 中文含义|\n",
    "| ---- | ---- | ----|\n",
    "|B|business day frequency|工作日频率|\n",
    "|C|custom business day frequency|自定义工作日频率|\n",
    "|D|calendar day frequency|自然日|\n",
    "|W|weekly frequency|每周|\n",
    "|M|month end frequency|月末|\n",
    "|SM|semi-month end frequency (15th and end of month)|半月末|\n",
    "|BM|business month end frequency|每月最后一个工作日|\n",
    "|CBM|custom business month end frequency|每月最后一个工作日，工作日是自定义的|\n",
    "|MS|month start frequency|每月初（第一天）|\n",
    "|SMS|semi-month start frequency (1st and 15th)|半月初（第一天）|\n",
    "|BMS|business month start frequency|每月第一个工作日|\n",
    "|CBMS|custom business month start frequency|每月第一个工作日，工作日是自定义的|\n",
    "|Q|quarter end frequency|每季末|\n",
    "|BQ|business quarter end frequency|每季左后一个工作日|\n",
    "|QS|quarter start frequency|每季初（第一天）|\n",
    "|BQS|business quarter start frequency|每季第一个工作日|\n",
    "|A,|Y year end frequency|每年末|\n",
    "|BA,|BY business year end frequency||\n",
    "|AS,|YS year start frequency||\n",
    "|BAS,|BYS business year start frequency||\n",
    "|BH|business hour frequency||\n",
    "|H|hourly frequency||\n",
    "|T,|min minutely frequency||\n",
    "|S|secondly frequency||\n",
    "|L,|ms milliseconds||\n",
    "|U,|us microseconds||\n",
    "|N|nanoseconds||\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 19.8.7 Combining Aliases"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "As we have seen previously, the alias and the offset instance are fungible in most functions:\n",
    "\n",
    "频率的别名和频率对象是可以相互替换的"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 110,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "DatetimeIndex(['2011-01-03', '2011-01-04', '2011-01-05', '2011-01-06',\n",
       "               '2011-01-07'],\n",
       "              dtype='datetime64[ns]', freq='B')"
      ]
     },
     "execution_count": 110,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "start = '2011-01-01'\n",
    "pd.date_range(start, periods=5, freq='B')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 111,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "DatetimeIndex(['2011-01-03', '2011-01-04', '2011-01-05', '2011-01-06',\n",
       "               '2011-01-07'],\n",
       "              dtype='datetime64[ns]', freq='B')"
      ]
     },
     "execution_count": 111,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.date_range(start, periods=5, freq=BDay())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 112,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "DatetimeIndex(['2011-01-01 00:00:00', '2011-01-01 02:20:00',\n",
       "               '2011-01-01 04:40:00', '2011-01-01 07:00:00',\n",
       "               '2011-01-01 09:20:00', '2011-01-01 11:40:00',\n",
       "               '2011-01-01 14:00:00', '2011-01-01 16:20:00',\n",
       "               '2011-01-01 18:40:00', '2011-01-01 21:00:00'],\n",
       "              dtype='datetime64[ns]', freq='140T')"
      ]
     },
     "execution_count": 112,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.date_range(start, periods=10, freq='2h20min')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 113,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "DatetimeIndex([       '2011-01-01 00:00:00', '2011-01-02 00:00:00.000010',\n",
       "               '2011-01-03 00:00:00.000020', '2011-01-04 00:00:00.000030',\n",
       "               '2011-01-05 00:00:00.000040', '2011-01-06 00:00:00.000050',\n",
       "               '2011-01-07 00:00:00.000060', '2011-01-08 00:00:00.000070',\n",
       "               '2011-01-09 00:00:00.000080', '2011-01-10 00:00:00.000090'],\n",
       "              dtype='datetime64[ns]', freq='86400000010U')"
      ]
     },
     "execution_count": 113,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.date_range(start, periods=10, freq='1D10U')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 19.8.8 Anchored Offsets"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "For some frequencies you can specify an anchoring suffix:"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "|Alias|Description|含义|\n",
    "| ---- | ---- | ----|\n",
    "|W-SUN|weekly frequency (Sundays). Same as ‘W’||\n",
    "|W-MON|weekly frequency (Mondays)||\n",
    "|W-TUE|weekly frequency (Tuesdays)||\n",
    "|W-WED|weekly frequency (Wednesdays)||\n",
    "|W-THU|weekly frequency (Thursdays)||\n",
    "|W-FRI|weekly frequency (Fridays)||\n",
    "|W-SAT|weekly frequency (Saturdays)||\n",
    "|(B)Q(S)-DEC|quarterly frequency, year ends in December. Same as ‘Q’||\n",
    "|(B)Q(S)-JAN|quarterly frequency, year ends in January||\n",
    "|(B)Q(S)-FEB|quarterly frequency, year ends in February||\n",
    "|(B)Q(S)-MAR|quarterly frequency, year ends in March||\n",
    "|(B)Q(S)-APR|quarterly frequency, year ends in April||\n",
    "|(B)Q(S)-MAY|quarterly frequency, year ends in May||\n",
    "|(B)Q(S)-JUN|quarterly frequency, year ends in June||\n",
    "|(B)Q(S)-JUL|quarterly frequency, year ends in July||\n",
    "|(B)Q(S)-AUG|quarterly frequency, year ends in August||\n",
    "|(B)Q(S)-SEP|quarterly frequency, year ends in September||\n",
    "|(B)Q(S)-OCT|quarterly frequency, year ends in October||\n",
    "|(B)Q(S)-NOV|quarterly frequency, year ends in November||\n",
    "|(B)A(S)-DEC|annual frequency, anchored end of December. Same as ‘A’||\n",
    "|(B)A(S)-JAN|annual frequency, anchored end of January||\n",
    "|(B)A(S)-FEB|annual frequency, anchored end of February||\n",
    "|(B)A(S)-MAR|annual frequency, anchored end of March||\n",
    "|(B)A(S)-APR|annual frequency, anchored end of April||\n",
    "|(B)A(S)-MAY|annual frequency, anchored end of May||\n",
    "|(B)A(S)-JUN|annual frequency, anchored end of June||\n",
    "|(B)A(S)-JUL|annual frequency, anchored end of July||\n",
    "|(B)A(S)-AUG|annual frequency, anchored end of August||\n",
    "|(B)A(S)-SEP|annual frequency, anchored end of September||\n",
    "|(B)A(S)-OCT|annual frequency, anchored end of October||\n",
    "|(B)A(S)-NOV|annual frequency, anchored end of November||"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "These can be used as arguments to date_range, bdate_range, constructors for DatetimeIndex, as well as various other timeseries-related functions in pandas."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 19.8.9 Anchored Offset Semantics"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "For those offsets that are anchored to the start or end of specific frequency (MonthEnd, MonthBegin, WeekEnd, etc) the following rules apply to rolling forward and backwards."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "When n is not 0, if the given date is not on an anchor point（锚点）, it snapped to the next(previous) anchor point（滑到下一个锚点）, and moved |n|-1 additional steps forwards or backwards."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 115,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Timestamp('2014-02-01 00:00:00')"
      ]
     },
     "execution_count": 115,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 下一个锚点是2014-02-01\n",
    "pd.Timestamp('2014-01-02') + MonthBegin(n=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 116,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Timestamp('2014-01-31 00:00:00')"
      ]
     },
     "execution_count": 116,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 下一个锚点是2014-01-31\n",
    "pd.Timestamp('2014-01-02') + MonthEnd(n=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 117,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Timestamp('2014-01-01 00:00:00')"
      ]
     },
     "execution_count": 117,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 上一个锚点是2014-01-01\n",
    "pd.Timestamp('2014-01-02') - MonthBegin(n=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 119,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Timestamp('2013-12-31 00:00:00')"
      ]
     },
     "execution_count": 119,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 上一个锚点是2013-12-31\n",
    "pd.Timestamp('2014-01-02') - MonthEnd(n=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 120,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Timestamp('2014-05-01 00:00:00')"
      ]
     },
     "execution_count": 120,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 下一个锚点是2014-02-01，然后再往前移动3步，则最终的结果为2014-05-01\n",
    "pd.Timestamp('2014-01-02') + MonthBegin(n=4)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "If the given date is on an anchor point, it is moved |n| points forwards or backwards.\n",
    "\n",
    "注意：这里是移动n个点，而上一种情况是移动|n|-1步"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 121,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Timestamp('2014-02-01 00:00:00')"
      ]
     },
     "execution_count": 121,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 2014-01-01是锚点，往前移动一步为2014-02-01\n",
    "pd.Timestamp('2014-01-01') + MonthBegin(n=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 122,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Timestamp('2014-02-28 00:00:00')"
      ]
     },
     "execution_count": 122,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.Timestamp('2014-01-31') + MonthEnd(n=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 123,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Timestamp('2013-12-01 00:00:00')"
      ]
     },
     "execution_count": 123,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.Timestamp('2014-01-01') - MonthBegin(n=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 124,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Timestamp('2013-12-31 00:00:00')"
      ]
     },
     "execution_count": 124,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.Timestamp('2014-01-31') - MonthEnd(n=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 125,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Timestamp('2013-10-01 00:00:00')"
      ]
     },
     "execution_count": 125,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.Timestamp('2014-01-31') - MonthBegin(n=4)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "For the case when n=0, the date is not moved if on an anchor point, otherwise it is rolled forward to the next anchor\n",
    "point."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 126,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Timestamp('2014-02-01 00:00:00')"
      ]
     },
     "execution_count": 126,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.Timestamp('2014-01-02') + MonthBegin(n=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 127,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Timestamp('2014-01-31 00:00:00')"
      ]
     },
     "execution_count": 127,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.Timestamp('2014-01-02') + MonthEnd(n=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 130,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Timestamp('2014-01-01 00:00:00')"
      ]
     },
     "execution_count": 130,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.Timestamp('2014-01-01') + MonthBegin(n=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 129,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Timestamp('2014-01-31 00:00:00')"
      ]
     },
     "execution_count": 129,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.Timestamp('2014-01-31') + MonthEnd(n=0)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 19.9 Time Series-Related Instance Methods"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 19.9.1 Shifting / Lagging"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 144,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2011-01-31   -0.322139\n",
       "2011-02-28   -1.241950\n",
       "2011-03-31   -0.808380\n",
       "2011-04-29    1.974188\n",
       "2011-05-31    0.947264\n",
       "Freq: BM, dtype: float64"
      ]
     },
     "execution_count": 144,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ts = ts[:5]\n",
    "ts"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 145,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2011-01-31         NaN\n",
       "2011-02-28   -0.322139\n",
       "2011-03-31   -1.241950\n",
       "2011-04-29   -0.808380\n",
       "2011-05-31    1.974188\n",
       "Freq: BM, dtype: float64"
      ]
     },
     "execution_count": 145,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ts.shift(1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The shift method accepts an freq argument which can accept a DateOffset class or other timedelta-like object\n",
    "or also a offset alias:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 149,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2011-02-07   -0.322139\n",
       "2011-03-07   -1.241950\n",
       "2011-04-07   -0.808380\n",
       "2011-05-06    1.974188\n",
       "2011-06-07    0.947264\n",
       "dtype: float64"
      ]
     },
     "execution_count": 149,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 将时间索引向前移动5个business day\n",
    "ts.shift(5, freq=BDay())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 150,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2011-06-30   -0.322139\n",
       "2011-07-29   -1.241950\n",
       "2011-08-31   -0.808380\n",
       "2011-09-30    1.974188\n",
       "2011-10-31    0.947264\n",
       "Freq: BM, dtype: float64"
      ]
     },
     "execution_count": 150,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ts.shift(5, freq='BM')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Rather than changing the alignment of the data and the index, DataFrame and Series objects also have a tshift\n",
    "convenience method that changes all the dates in the index by a specified number of offsets:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 151,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2011-02-05   -0.322139\n",
       "2011-03-05   -1.241950\n",
       "2011-04-05   -0.808380\n",
       "2011-05-04    1.974188\n",
       "2011-06-05    0.947264\n",
       "dtype: float64"
      ]
     },
     "execution_count": 151,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ts.tshift(5, freq='D')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Note that with tshift, the leading entry is no longer NaN because the data is not being realigned."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 19.9.2 Frequency Conversion"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 155,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2010-01-01    1.510654\n",
       "2010-01-06    0.545416\n",
       "2010-01-11    0.801161\n",
       "Freq: 3B, dtype: float64"
      ]
     },
     "execution_count": 155,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dr = pd.date_range('1/1/2010', periods=3, freq=3*BDay())\n",
    "ts = pd.Series(randn(3), index=dr)\n",
    "ts"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 156,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2010-01-01    1.510654\n",
       "2010-01-04         NaN\n",
       "2010-01-05         NaN\n",
       "2010-01-06    0.545416\n",
       "2010-01-07         NaN\n",
       "2010-01-08         NaN\n",
       "2010-01-11    0.801161\n",
       "Freq: B, dtype: float64"
      ]
     },
     "execution_count": 156,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ts.asfreq(BDay())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "asfreq provides a further convenience so you can specify an interpolation method for any gaps that may appear after the frequency conversion"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 158,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2010-01-01    1.510654\n",
       "2010-01-04    1.510654\n",
       "2010-01-05    1.510654\n",
       "2010-01-06    0.545416\n",
       "2010-01-07    0.545416\n",
       "2010-01-08    0.545416\n",
       "2010-01-11    0.801161\n",
       "Freq: B, dtype: float64"
      ]
     },
     "execution_count": 158,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ts.asfreq(BDay(), method='pad')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 19.9.3 Filling Forward / Backward"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Related to asfreq and reindex is the fillna function documented in the missing data section."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 19.9.4 Converting to Python Datetimes"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "DatetimeIndex can be converted to an array of Python native datetime.datetime objects using the to_pydatetime method.\n",
    "\n",
    "该方法可以和to_datetime对比来记忆"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 19.10 Resampling"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Pandas has a simple, powerful, and efficient functionality for performing resampling operations during **frequency** conversion (e.g., converting secondly data into 5-minutely data). This is extremely common in, but not limited to,\n",
    "financial applications."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    ".resample() is a time-based groupby, followed by a reduction method on each of its groups. See some cookbook examples for some advanced strategies\n",
    "\n",
    "Starting in version 0.18.1, the resample() function can be used directly from DataFrameGroupBy objects, see the groupby docs.\n",
    "\n",
    "从这个角度来看，resample只能够将频"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 19.10.1 Basics"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "100"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rng = pd.date_range('1/1/2012', periods=100, freq='S')\n",
    "len(rng)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false,
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2012-01-01 00:00:00    135\n",
       "2012-01-01 00:00:01    475\n",
       "2012-01-01 00:00:02    261\n",
       "2012-01-01 00:00:03    171\n",
       "2012-01-01 00:00:04    345\n",
       "2012-01-01 00:00:05    290\n",
       "2012-01-01 00:00:06    333\n",
       "2012-01-01 00:00:07    357\n",
       "2012-01-01 00:00:08    481\n",
       "2012-01-01 00:00:09    340\n",
       "2012-01-01 00:00:10    287\n",
       "2012-01-01 00:00:11    232\n",
       "2012-01-01 00:00:12    340\n",
       "2012-01-01 00:00:13     99\n",
       "2012-01-01 00:00:14    440\n",
       "2012-01-01 00:00:15    371\n",
       "2012-01-01 00:00:16    164\n",
       "2012-01-01 00:00:17    358\n",
       "2012-01-01 00:00:18    205\n",
       "2012-01-01 00:00:19    347\n",
       "2012-01-01 00:00:20    492\n",
       "2012-01-01 00:00:21    314\n",
       "2012-01-01 00:00:22    324\n",
       "2012-01-01 00:00:23    489\n",
       "2012-01-01 00:00:24    200\n",
       "2012-01-01 00:00:25    184\n",
       "2012-01-01 00:00:26    119\n",
       "2012-01-01 00:00:27    263\n",
       "2012-01-01 00:00:28    108\n",
       "2012-01-01 00:00:29     52\n",
       "                      ... \n",
       "2012-01-01 00:01:10     92\n",
       "2012-01-01 00:01:11    375\n",
       "2012-01-01 00:01:12    174\n",
       "2012-01-01 00:01:13    380\n",
       "2012-01-01 00:01:14     24\n",
       "2012-01-01 00:01:15    408\n",
       "2012-01-01 00:01:16    498\n",
       "2012-01-01 00:01:17    425\n",
       "2012-01-01 00:01:18     29\n",
       "2012-01-01 00:01:19    358\n",
       "2012-01-01 00:01:20     11\n",
       "2012-01-01 00:01:21     57\n",
       "2012-01-01 00:01:22    466\n",
       "2012-01-01 00:01:23    226\n",
       "2012-01-01 00:01:24    158\n",
       "2012-01-01 00:01:25    126\n",
       "2012-01-01 00:01:26    379\n",
       "2012-01-01 00:01:27     36\n",
       "2012-01-01 00:01:28    256\n",
       "2012-01-01 00:01:29    412\n",
       "2012-01-01 00:01:30     90\n",
       "2012-01-01 00:01:31    267\n",
       "2012-01-01 00:01:32    318\n",
       "2012-01-01 00:01:33    496\n",
       "2012-01-01 00:01:34    429\n",
       "2012-01-01 00:01:35    414\n",
       "2012-01-01 00:01:36    469\n",
       "2012-01-01 00:01:37    464\n",
       "2012-01-01 00:01:38     96\n",
       "2012-01-01 00:01:39    237\n",
       "Freq: S, Length: 100, dtype: int32"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ts = pd.Series(np.random.randint(0, 500, len(rng)), index=rng)\n",
    "ts"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2012-01-01    26030\n",
       "Freq: 5T, dtype: int32"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ts.resample('5Min').sum()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 182,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2012-01-01    227.18\n",
       "Freq: 5T, dtype: float64"
      ]
     },
     "execution_count": 182,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ts.resample('5Min').mean()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>open</th>\n",
       "      <th>high</th>\n",
       "      <th>low</th>\n",
       "      <th>close</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2012-01-01</th>\n",
       "      <td>135</td>\n",
       "      <td>498</td>\n",
       "      <td>7</td>\n",
       "      <td>237</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "            open  high  low  close\n",
       "2012-01-01   135   498    7    237"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ts.resample('5Min').ohlc()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 169,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2012-01-01    490\n",
       "Freq: 5T, dtype: int32"
      ]
     },
     "execution_count": 169,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ts.resample('5Min').max()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "For downsampling, closed can be set to ‘left’ or ‘right’ to specify which end of the interval is closed:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 183,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2011-12-31 23:55:00    120.000000\n",
       "2012-01-01 00:00:00    228.262626\n",
       "Freq: 5T, dtype: float64"
      ]
     },
     "execution_count": 183,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ts.resample('5Min', closed='right').mean()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 184,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2012-01-01    227.18\n",
       "Freq: 5T, dtype: float64"
      ]
     },
     "execution_count": 184,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ts.resample('5Min', closed='left').mean()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 172,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2012-01-01    229.92\n",
       "Freq: 5T, dtype: float64"
      ]
     },
     "execution_count": 172,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ts.resample('5Min').mean() # by default label='left'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 173,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2012-01-01    229.92\n",
       "Freq: 5T, dtype: float64"
      ]
     },
     "execution_count": 173,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ts.resample('5Min', label='left').mean()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 174,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2012-01-01 00:00:01    229.92\n",
       "dtype: float64"
      ]
     },
     "execution_count": 174,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ts.resample('5Min', label='left', loffset='1s').mean()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 187,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2012-01-01     0\n",
       "2012-01-02     1\n",
       "2012-01-03     2\n",
       "2012-01-04     3\n",
       "2012-01-05     4\n",
       "2012-01-06     5\n",
       "2012-01-07     6\n",
       "2012-01-08     7\n",
       "2012-01-09     8\n",
       "2012-01-10     9\n",
       "2012-01-11    10\n",
       "2012-01-12    11\n",
       "2012-01-13    12\n",
       "2012-01-14    13\n",
       "2012-01-15    14\n",
       "2012-01-16    15\n",
       "2012-01-17    16\n",
       "2012-01-18    17\n",
       "2012-01-19    18\n",
       "2012-01-20    19\n",
       "2012-01-21    20\n",
       "2012-01-22    21\n",
       "2012-01-23    22\n",
       "2012-01-24    23\n",
       "2012-01-25    24\n",
       "2012-01-26    25\n",
       "2012-01-27    26\n",
       "2012-01-28    27\n",
       "2012-01-29    28\n",
       "2012-01-30    29\n",
       "              ..\n",
       "2012-03-02    61\n",
       "2012-03-03    62\n",
       "2012-03-04    63\n",
       "2012-03-05    64\n",
       "2012-03-06    65\n",
       "2012-03-07    66\n",
       "2012-03-08    67\n",
       "2012-03-09    68\n",
       "2012-03-10    69\n",
       "2012-03-11    70\n",
       "2012-03-12    71\n",
       "2012-03-13    72\n",
       "2012-03-14    73\n",
       "2012-03-15    74\n",
       "2012-03-16    75\n",
       "2012-03-17    76\n",
       "2012-03-18    77\n",
       "2012-03-19    78\n",
       "2012-03-20    79\n",
       "2012-03-21    80\n",
       "2012-03-22    81\n",
       "2012-03-23    82\n",
       "2012-03-24    83\n",
       "2012-03-25    84\n",
       "2012-03-26    85\n",
       "2012-03-27    86\n",
       "2012-03-28    87\n",
       "2012-03-29    88\n",
       "2012-03-30    89\n",
       "2012-03-31    90\n",
       "Freq: D, Length: 91, dtype: int64"
      ]
     },
     "execution_count": 187,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rng2 = pd.date_range('1/1/2012', end='3/31/2012', freq='D')\n",
    "ts2 = pd.Series(range(len(rng2)), index=rng2)\n",
    "ts2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 186,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2012-01-31    30\n",
       "2012-02-29    59\n",
       "2012-03-31    90\n",
       "Freq: M, dtype: int64"
      ]
     },
     "execution_count": 186,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# default: label='right', closed='right'\n",
    "ts2.resample('M').max()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 188,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2011-12-31    13\n",
       "2012-01-15    29\n",
       "2012-01-31    44\n",
       "2012-02-15    58\n",
       "2012-02-29    73\n",
       "2012-03-15    89\n",
       "2012-03-31    90\n",
       "Freq: SM-15, dtype: int64"
      ]
     },
     "execution_count": 188,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# default: label='left', closed='left'\n",
    "ts2.resample('SM').max()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 19.10.2 Upsampling"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 190,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "DatetimeIndexResampler [freq=<250 * Millis>, axis=0, closed=left, label=left, convention=start, base=0]"
      ]
     },
     "execution_count": 190,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# from secondly to every 250 milliseconds\n",
    "ts[:2].resample('250L')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 191,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2012-01-01 00:00:00.000    120.0\n",
       "2012-01-01 00:00:00.250      NaN\n",
       "2012-01-01 00:00:00.500      NaN\n",
       "2012-01-01 00:00:00.750      NaN\n",
       "2012-01-01 00:00:01.000    405.0\n",
       "Freq: 250L, dtype: float64"
      ]
     },
     "execution_count": 191,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ts[:2].resample('250L').asfreq()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 192,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2012-01-01 00:00:00.000    120\n",
       "2012-01-01 00:00:00.250    120\n",
       "2012-01-01 00:00:00.500    120\n",
       "2012-01-01 00:00:00.750    120\n",
       "2012-01-01 00:00:01.000    405\n",
       "Freq: 250L, dtype: int32"
      ]
     },
     "execution_count": 192,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ts[:2].resample('250L').ffill()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 193,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2012-01-01 00:00:00.000    120.0\n",
       "2012-01-01 00:00:00.250    120.0\n",
       "2012-01-01 00:00:00.500    120.0\n",
       "2012-01-01 00:00:00.750      NaN\n",
       "2012-01-01 00:00:01.000    405.0\n",
       "Freq: 250L, dtype: float64"
      ]
     },
     "execution_count": 193,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ts[:2].resample('250L').ffill(limit=2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 19.10.3 Sparse Resampling"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 195,
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "DatetimeIndex(['2014-01-01 00:00:01', '2014-01-02 00:00:01',\n",
       "               '2014-01-03 00:00:01', '2014-01-04 00:00:01',\n",
       "               '2014-01-05 00:00:01', '2014-01-06 00:00:01',\n",
       "               '2014-01-07 00:00:01', '2014-01-08 00:00:01',\n",
       "               '2014-01-09 00:00:01', '2014-01-10 00:00:01',\n",
       "               '2014-01-11 00:00:01', '2014-01-12 00:00:01',\n",
       "               '2014-01-13 00:00:01', '2014-01-14 00:00:01',\n",
       "               '2014-01-15 00:00:01', '2014-01-16 00:00:01',\n",
       "               '2014-01-17 00:00:01', '2014-01-18 00:00:01',\n",
       "               '2014-01-19 00:00:01', '2014-01-20 00:00:01',\n",
       "               '2014-01-21 00:00:01', '2014-01-22 00:00:01',\n",
       "               '2014-01-23 00:00:01', '2014-01-24 00:00:01',\n",
       "               '2014-01-25 00:00:01', '2014-01-26 00:00:01',\n",
       "               '2014-01-27 00:00:01', '2014-01-28 00:00:01',\n",
       "               '2014-01-29 00:00:01', '2014-01-30 00:00:01',\n",
       "               '2014-01-31 00:00:01', '2014-02-01 00:00:01',\n",
       "               '2014-02-02 00:00:01', '2014-02-03 00:00:01',\n",
       "               '2014-02-04 00:00:01', '2014-02-05 00:00:01',\n",
       "               '2014-02-06 00:00:01', '2014-02-07 00:00:01',\n",
       "               '2014-02-08 00:00:01', '2014-02-09 00:00:01',\n",
       "               '2014-02-10 00:00:01', '2014-02-11 00:00:01',\n",
       "               '2014-02-12 00:00:01', '2014-02-13 00:00:01',\n",
       "               '2014-02-14 00:00:01', '2014-02-15 00:00:01',\n",
       "               '2014-02-16 00:00:01', '2014-02-17 00:00:01',\n",
       "               '2014-02-18 00:00:01', '2014-02-19 00:00:01',\n",
       "               '2014-02-20 00:00:01', '2014-02-21 00:00:01',\n",
       "               '2014-02-22 00:00:01', '2014-02-23 00:00:01',\n",
       "               '2014-02-24 00:00:01', '2014-02-25 00:00:01',\n",
       "               '2014-02-26 00:00:01', '2014-02-27 00:00:01',\n",
       "               '2014-02-28 00:00:01', '2014-03-01 00:00:01',\n",
       "               '2014-03-02 00:00:01', '2014-03-03 00:00:01',\n",
       "               '2014-03-04 00:00:01', '2014-03-05 00:00:01',\n",
       "               '2014-03-06 00:00:01', '2014-03-07 00:00:01',\n",
       "               '2014-03-08 00:00:01', '2014-03-09 00:00:01',\n",
       "               '2014-03-10 00:00:01', '2014-03-11 00:00:01',\n",
       "               '2014-03-12 00:00:01', '2014-03-13 00:00:01',\n",
       "               '2014-03-14 00:00:01', '2014-03-15 00:00:01',\n",
       "               '2014-03-16 00:00:01', '2014-03-17 00:00:01',\n",
       "               '2014-03-18 00:00:01', '2014-03-19 00:00:01',\n",
       "               '2014-03-20 00:00:01', '2014-03-21 00:00:01',\n",
       "               '2014-03-22 00:00:01', '2014-03-23 00:00:01',\n",
       "               '2014-03-24 00:00:01', '2014-03-25 00:00:01',\n",
       "               '2014-03-26 00:00:01', '2014-03-27 00:00:01',\n",
       "               '2014-03-28 00:00:01', '2014-03-29 00:00:01',\n",
       "               '2014-03-30 00:00:01', '2014-03-31 00:00:01',\n",
       "               '2014-04-01 00:00:01', '2014-04-02 00:00:01',\n",
       "               '2014-04-03 00:00:01', '2014-04-04 00:00:01',\n",
       "               '2014-04-05 00:00:01', '2014-04-06 00:00:01',\n",
       "               '2014-04-07 00:00:01', '2014-04-08 00:00:01',\n",
       "               '2014-04-09 00:00:01', '2014-04-10 00:00:01'],\n",
       "              dtype='datetime64[ns]', freq='D')"
      ]
     },
     "execution_count": 195,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rng = pd.date_range('2014-1-1', periods=100, freq='D') + pd.Timedelta('1s')\n",
    "rng"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 198,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2014-01-01 00:00:01     0\n",
       "2014-01-02 00:00:01     1\n",
       "2014-01-03 00:00:01     2\n",
       "2014-01-04 00:00:01     3\n",
       "2014-01-05 00:00:01     4\n",
       "2014-01-06 00:00:01     5\n",
       "2014-01-07 00:00:01     6\n",
       "2014-01-08 00:00:01     7\n",
       "2014-01-09 00:00:01     8\n",
       "2014-01-10 00:00:01     9\n",
       "2014-01-11 00:00:01    10\n",
       "2014-01-12 00:00:01    11\n",
       "2014-01-13 00:00:01    12\n",
       "2014-01-14 00:00:01    13\n",
       "2014-01-15 00:00:01    14\n",
       "2014-01-16 00:00:01    15\n",
       "2014-01-17 00:00:01    16\n",
       "2014-01-18 00:00:01    17\n",
       "2014-01-19 00:00:01    18\n",
       "2014-01-20 00:00:01    19\n",
       "2014-01-21 00:00:01    20\n",
       "2014-01-22 00:00:01    21\n",
       "2014-01-23 00:00:01    22\n",
       "2014-01-24 00:00:01    23\n",
       "2014-01-25 00:00:01    24\n",
       "2014-01-26 00:00:01    25\n",
       "2014-01-27 00:00:01    26\n",
       "2014-01-28 00:00:01    27\n",
       "2014-01-29 00:00:01    28\n",
       "2014-01-30 00:00:01    29\n",
       "                       ..\n",
       "2014-03-12 00:00:01    70\n",
       "2014-03-13 00:00:01    71\n",
       "2014-03-14 00:00:01    72\n",
       "2014-03-15 00:00:01    73\n",
       "2014-03-16 00:00:01    74\n",
       "2014-03-17 00:00:01    75\n",
       "2014-03-18 00:00:01    76\n",
       "2014-03-19 00:00:01    77\n",
       "2014-03-20 00:00:01    78\n",
       "2014-03-21 00:00:01    79\n",
       "2014-03-22 00:00:01    80\n",
       "2014-03-23 00:00:01    81\n",
       "2014-03-24 00:00:01    82\n",
       "2014-03-25 00:00:01    83\n",
       "2014-03-26 00:00:01    84\n",
       "2014-03-27 00:00:01    85\n",
       "2014-03-28 00:00:01    86\n",
       "2014-03-29 00:00:01    87\n",
       "2014-03-30 00:00:01    88\n",
       "2014-03-31 00:00:01    89\n",
       "2014-04-01 00:00:01    90\n",
       "2014-04-02 00:00:01    91\n",
       "2014-04-03 00:00:01    92\n",
       "2014-04-04 00:00:01    93\n",
       "2014-04-05 00:00:01    94\n",
       "2014-04-06 00:00:01    95\n",
       "2014-04-07 00:00:01    96\n",
       "2014-04-08 00:00:01    97\n",
       "2014-04-09 00:00:01    98\n",
       "2014-04-10 00:00:01    99\n",
       "Freq: D, Length: 100, dtype: int64"
      ]
     },
     "execution_count": 198,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ts = pd.Series(range(100), index=rng)\n",
    "ts"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 199,
   "metadata": {
    "collapsed": false,
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2014-01-01 00:00:00     0\n",
       "2014-01-01 00:03:00     0\n",
       "2014-01-01 00:06:00     0\n",
       "2014-01-01 00:09:00     0\n",
       "2014-01-01 00:12:00     0\n",
       "2014-01-01 00:15:00     0\n",
       "2014-01-01 00:18:00     0\n",
       "2014-01-01 00:21:00     0\n",
       "2014-01-01 00:24:00     0\n",
       "2014-01-01 00:27:00     0\n",
       "2014-01-01 00:30:00     0\n",
       "2014-01-01 00:33:00     0\n",
       "2014-01-01 00:36:00     0\n",
       "2014-01-01 00:39:00     0\n",
       "2014-01-01 00:42:00     0\n",
       "2014-01-01 00:45:00     0\n",
       "2014-01-01 00:48:00     0\n",
       "2014-01-01 00:51:00     0\n",
       "2014-01-01 00:54:00     0\n",
       "2014-01-01 00:57:00     0\n",
       "2014-01-01 01:00:00     0\n",
       "2014-01-01 01:03:00     0\n",
       "2014-01-01 01:06:00     0\n",
       "2014-01-01 01:09:00     0\n",
       "2014-01-01 01:12:00     0\n",
       "2014-01-01 01:15:00     0\n",
       "2014-01-01 01:18:00     0\n",
       "2014-01-01 01:21:00     0\n",
       "2014-01-01 01:24:00     0\n",
       "2014-01-01 01:27:00     0\n",
       "                       ..\n",
       "2014-04-09 22:33:00     0\n",
       "2014-04-09 22:36:00     0\n",
       "2014-04-09 22:39:00     0\n",
       "2014-04-09 22:42:00     0\n",
       "2014-04-09 22:45:00     0\n",
       "2014-04-09 22:48:00     0\n",
       "2014-04-09 22:51:00     0\n",
       "2014-04-09 22:54:00     0\n",
       "2014-04-09 22:57:00     0\n",
       "2014-04-09 23:00:00     0\n",
       "2014-04-09 23:03:00     0\n",
       "2014-04-09 23:06:00     0\n",
       "2014-04-09 23:09:00     0\n",
       "2014-04-09 23:12:00     0\n",
       "2014-04-09 23:15:00     0\n",
       "2014-04-09 23:18:00     0\n",
       "2014-04-09 23:21:00     0\n",
       "2014-04-09 23:24:00     0\n",
       "2014-04-09 23:27:00     0\n",
       "2014-04-09 23:30:00     0\n",
       "2014-04-09 23:33:00     0\n",
       "2014-04-09 23:36:00     0\n",
       "2014-04-09 23:39:00     0\n",
       "2014-04-09 23:42:00     0\n",
       "2014-04-09 23:45:00     0\n",
       "2014-04-09 23:48:00     0\n",
       "2014-04-09 23:51:00     0\n",
       "2014-04-09 23:54:00     0\n",
       "2014-04-09 23:57:00     0\n",
       "2014-04-10 00:00:00    99\n",
       "Freq: 3T, Length: 47521, dtype: int64"
      ]
     },
     "execution_count": 199,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ts.resample('3T').sum()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 200,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from functools import partial\n",
    "from pandas.tseries.frequencies import to_offset\n",
    "def round(t, freq):\n",
    "    freq = to_offset(freq)\n",
    "    return pd.Timestamp((t.value // freq.delta.value)*freq.delta.value)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 201,
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2014-01-01     0\n",
       "2014-01-02     1\n",
       "2014-01-03     2\n",
       "2014-01-04     3\n",
       "2014-01-05     4\n",
       "2014-01-06     5\n",
       "2014-01-07     6\n",
       "2014-01-08     7\n",
       "2014-01-09     8\n",
       "2014-01-10     9\n",
       "2014-01-11    10\n",
       "2014-01-12    11\n",
       "2014-01-13    12\n",
       "2014-01-14    13\n",
       "2014-01-15    14\n",
       "2014-01-16    15\n",
       "2014-01-17    16\n",
       "2014-01-18    17\n",
       "2014-01-19    18\n",
       "2014-01-20    19\n",
       "2014-01-21    20\n",
       "2014-01-22    21\n",
       "2014-01-23    22\n",
       "2014-01-24    23\n",
       "2014-01-25    24\n",
       "2014-01-26    25\n",
       "2014-01-27    26\n",
       "2014-01-28    27\n",
       "2014-01-29    28\n",
       "2014-01-30    29\n",
       "              ..\n",
       "2014-03-12    70\n",
       "2014-03-13    71\n",
       "2014-03-14    72\n",
       "2014-03-15    73\n",
       "2014-03-16    74\n",
       "2014-03-17    75\n",
       "2014-03-18    76\n",
       "2014-03-19    77\n",
       "2014-03-20    78\n",
       "2014-03-21    79\n",
       "2014-03-22    80\n",
       "2014-03-23    81\n",
       "2014-03-24    82\n",
       "2014-03-25    83\n",
       "2014-03-26    84\n",
       "2014-03-27    85\n",
       "2014-03-28    86\n",
       "2014-03-29    87\n",
       "2014-03-30    88\n",
       "2014-03-31    89\n",
       "2014-04-01    90\n",
       "2014-04-02    91\n",
       "2014-04-03    92\n",
       "2014-04-04    93\n",
       "2014-04-05    94\n",
       "2014-04-06    95\n",
       "2014-04-07    96\n",
       "2014-04-08    97\n",
       "2014-04-09    98\n",
       "2014-04-10    99\n",
       "Length: 100, dtype: int64"
      ]
     },
     "execution_count": 201,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ts.groupby(partial(round, freq='3T')).sum()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 19.10.4 Aggregation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 203,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "df = pd.DataFrame(np.random.randn(1000, 3),\n",
    "                  index=pd.date_range('1/1/2012', freq='S', periods=1000),\n",
    "                  columns=['A', 'B', 'C'])\n",
    "r = df.resample('3T')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 204,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2012-01-01 00:00:00</th>\n",
       "      <td>-0.118908</td>\n",
       "      <td>-0.062350</td>\n",
       "      <td>-0.052676</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2012-01-01 00:03:00</th>\n",
       "      <td>0.019445</td>\n",
       "      <td>0.036599</td>\n",
       "      <td>-0.120844</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2012-01-01 00:06:00</th>\n",
       "      <td>0.040693</td>\n",
       "      <td>0.032505</td>\n",
       "      <td>0.032937</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2012-01-01 00:09:00</th>\n",
       "      <td>-0.017517</td>\n",
       "      <td>-0.025258</td>\n",
       "      <td>0.038029</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2012-01-01 00:12:00</th>\n",
       "      <td>0.016157</td>\n",
       "      <td>-0.008374</td>\n",
       "      <td>-0.007247</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2012-01-01 00:15:00</th>\n",
       "      <td>-0.016999</td>\n",
       "      <td>0.128331</td>\n",
       "      <td>0.011716</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                            A         B         C\n",
       "2012-01-01 00:00:00 -0.118908 -0.062350 -0.052676\n",
       "2012-01-01 00:03:00  0.019445  0.036599 -0.120844\n",
       "2012-01-01 00:06:00  0.040693  0.032505  0.032937\n",
       "2012-01-01 00:09:00 -0.017517 -0.025258  0.038029\n",
       "2012-01-01 00:12:00  0.016157 -0.008374 -0.007247\n",
       "2012-01-01 00:15:00 -0.016999  0.128331  0.011716"
      ]
     },
     "execution_count": 204,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.mean()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 205,
   "metadata": {
    "collapsed": false,
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2012-01-01 00:00:00   -0.118908\n",
       "2012-01-01 00:03:00    0.019445\n",
       "2012-01-01 00:06:00    0.040693\n",
       "2012-01-01 00:09:00   -0.017517\n",
       "2012-01-01 00:12:00    0.016157\n",
       "2012-01-01 00:15:00   -0.016999\n",
       "Freq: 3T, Name: A, dtype: float64"
      ]
     },
     "execution_count": 205,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r['A'].mean()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 206,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2012-01-01 00:00:00</th>\n",
       "      <td>-0.118908</td>\n",
       "      <td>-0.062350</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2012-01-01 00:03:00</th>\n",
       "      <td>0.019445</td>\n",
       "      <td>0.036599</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2012-01-01 00:06:00</th>\n",
       "      <td>0.040693</td>\n",
       "      <td>0.032505</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2012-01-01 00:09:00</th>\n",
       "      <td>-0.017517</td>\n",
       "      <td>-0.025258</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2012-01-01 00:12:00</th>\n",
       "      <td>0.016157</td>\n",
       "      <td>-0.008374</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2012-01-01 00:15:00</th>\n",
       "      <td>-0.016999</td>\n",
       "      <td>0.128331</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                            A         B\n",
       "2012-01-01 00:00:00 -0.118908 -0.062350\n",
       "2012-01-01 00:03:00  0.019445  0.036599\n",
       "2012-01-01 00:06:00  0.040693  0.032505\n",
       "2012-01-01 00:09:00 -0.017517 -0.025258\n",
       "2012-01-01 00:12:00  0.016157 -0.008374\n",
       "2012-01-01 00:15:00 -0.016999  0.128331"
      ]
     },
     "execution_count": 206,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r[['A','B']].mean()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 207,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>sum</th>\n",
       "      <th>mean</th>\n",
       "      <th>std</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2012-01-01 00:00:00</th>\n",
       "      <td>-21.403502</td>\n",
       "      <td>-0.118908</td>\n",
       "      <td>0.990016</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2012-01-01 00:03:00</th>\n",
       "      <td>3.500088</td>\n",
       "      <td>0.019445</td>\n",
       "      <td>0.904340</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2012-01-01 00:06:00</th>\n",
       "      <td>7.324659</td>\n",
       "      <td>0.040693</td>\n",
       "      <td>1.043424</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2012-01-01 00:09:00</th>\n",
       "      <td>-3.153064</td>\n",
       "      <td>-0.017517</td>\n",
       "      <td>1.001389</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2012-01-01 00:12:00</th>\n",
       "      <td>2.908233</td>\n",
       "      <td>0.016157</td>\n",
       "      <td>1.056619</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2012-01-01 00:15:00</th>\n",
       "      <td>-1.699902</td>\n",
       "      <td>-0.016999</td>\n",
       "      <td>0.937193</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                           sum      mean       std\n",
       "2012-01-01 00:00:00 -21.403502 -0.118908  0.990016\n",
       "2012-01-01 00:03:00   3.500088  0.019445  0.904340\n",
       "2012-01-01 00:06:00   7.324659  0.040693  1.043424\n",
       "2012-01-01 00:09:00  -3.153064 -0.017517  1.001389\n",
       "2012-01-01 00:12:00   2.908233  0.016157  1.056619\n",
       "2012-01-01 00:15:00  -1.699902 -0.016999  0.937193"
      ]
     },
     "execution_count": 207,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r['A'].agg([np.sum, np.mean, np.std])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 208,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr th {\n",
       "        text-align: left;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th colspan=\"2\" halign=\"left\">A</th>\n",
       "      <th colspan=\"2\" halign=\"left\">B</th>\n",
       "      <th colspan=\"2\" halign=\"left\">C</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th>sum</th>\n",
       "      <th>mean</th>\n",
       "      <th>sum</th>\n",
       "      <th>mean</th>\n",
       "      <th>sum</th>\n",
       "      <th>mean</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2012-01-01 00:00:00</th>\n",
       "      <td>-21.403502</td>\n",
       "      <td>-0.118908</td>\n",
       "      <td>-11.222965</td>\n",
       "      <td>-0.062350</td>\n",
       "      <td>-9.481728</td>\n",
       "      <td>-0.052676</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2012-01-01 00:03:00</th>\n",
       "      <td>3.500088</td>\n",
       "      <td>0.019445</td>\n",
       "      <td>6.587825</td>\n",
       "      <td>0.036599</td>\n",
       "      <td>-21.751967</td>\n",
       "      <td>-0.120844</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2012-01-01 00:06:00</th>\n",
       "      <td>7.324659</td>\n",
       "      <td>0.040693</td>\n",
       "      <td>5.850915</td>\n",
       "      <td>0.032505</td>\n",
       "      <td>5.928581</td>\n",
       "      <td>0.032937</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2012-01-01 00:09:00</th>\n",
       "      <td>-3.153064</td>\n",
       "      <td>-0.017517</td>\n",
       "      <td>-4.546449</td>\n",
       "      <td>-0.025258</td>\n",
       "      <td>6.845245</td>\n",
       "      <td>0.038029</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2012-01-01 00:12:00</th>\n",
       "      <td>2.908233</td>\n",
       "      <td>0.016157</td>\n",
       "      <td>-1.507251</td>\n",
       "      <td>-0.008374</td>\n",
       "      <td>-1.304478</td>\n",
       "      <td>-0.007247</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2012-01-01 00:15:00</th>\n",
       "      <td>-1.699902</td>\n",
       "      <td>-0.016999</td>\n",
       "      <td>12.833106</td>\n",
       "      <td>0.128331</td>\n",
       "      <td>1.171576</td>\n",
       "      <td>0.011716</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                             A                    B                    C  \\\n",
       "                           sum      mean        sum      mean        sum   \n",
       "2012-01-01 00:00:00 -21.403502 -0.118908 -11.222965 -0.062350  -9.481728   \n",
       "2012-01-01 00:03:00   3.500088  0.019445   6.587825  0.036599 -21.751967   \n",
       "2012-01-01 00:06:00   7.324659  0.040693   5.850915  0.032505   5.928581   \n",
       "2012-01-01 00:09:00  -3.153064 -0.017517  -4.546449 -0.025258   6.845245   \n",
       "2012-01-01 00:12:00   2.908233  0.016157  -1.507251 -0.008374  -1.304478   \n",
       "2012-01-01 00:15:00  -1.699902 -0.016999  12.833106  0.128331   1.171576   \n",
       "\n",
       "                               \n",
       "                         mean  \n",
       "2012-01-01 00:00:00 -0.052676  \n",
       "2012-01-01 00:03:00 -0.120844  \n",
       "2012-01-01 00:06:00  0.032937  \n",
       "2012-01-01 00:09:00  0.038029  \n",
       "2012-01-01 00:12:00 -0.007247  \n",
       "2012-01-01 00:15:00  0.011716  "
      ]
     },
     "execution_count": 208,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.agg([np.sum, np.mean])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 209,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2012-01-01 00:00:00</th>\n",
       "      <td>-21.403502</td>\n",
       "      <td>1.013514</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2012-01-01 00:03:00</th>\n",
       "      <td>3.500088</td>\n",
       "      <td>1.018236</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2012-01-01 00:06:00</th>\n",
       "      <td>7.324659</td>\n",
       "      <td>0.887138</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2012-01-01 00:09:00</th>\n",
       "      <td>-3.153064</td>\n",
       "      <td>1.099908</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2012-01-01 00:12:00</th>\n",
       "      <td>2.908233</td>\n",
       "      <td>1.045755</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2012-01-01 00:15:00</th>\n",
       "      <td>-1.699902</td>\n",
       "      <td>0.943251</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                             A         B\n",
       "2012-01-01 00:00:00 -21.403502  1.013514\n",
       "2012-01-01 00:03:00   3.500088  1.018236\n",
       "2012-01-01 00:06:00   7.324659  0.887138\n",
       "2012-01-01 00:09:00  -3.153064  1.099908\n",
       "2012-01-01 00:12:00   2.908233  1.045755\n",
       "2012-01-01 00:15:00  -1.699902  0.943251"
      ]
     },
     "execution_count": 209,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.agg({'A' : np.sum,\n",
    "       'B' : lambda x: np.std(x, ddof=1)})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 210,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2012-01-01 00:00:00</th>\n",
       "      <td>-21.403502</td>\n",
       "      <td>1.013514</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2012-01-01 00:03:00</th>\n",
       "      <td>3.500088</td>\n",
       "      <td>1.018236</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2012-01-01 00:06:00</th>\n",
       "      <td>7.324659</td>\n",
       "      <td>0.887138</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2012-01-01 00:09:00</th>\n",
       "      <td>-3.153064</td>\n",
       "      <td>1.099908</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2012-01-01 00:12:00</th>\n",
       "      <td>2.908233</td>\n",
       "      <td>1.045755</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2012-01-01 00:15:00</th>\n",
       "      <td>-1.699902</td>\n",
       "      <td>0.943251</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                             A         B\n",
       "2012-01-01 00:00:00 -21.403502  1.013514\n",
       "2012-01-01 00:03:00   3.500088  1.018236\n",
       "2012-01-01 00:06:00   7.324659  0.887138\n",
       "2012-01-01 00:09:00  -3.153064  1.099908\n",
       "2012-01-01 00:12:00   2.908233  1.045755\n",
       "2012-01-01 00:15:00  -1.699902  0.943251"
      ]
     },
     "execution_count": 210,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.agg({'A' : 'sum', 'B' : 'std'})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 211,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr th {\n",
       "        text-align: left;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th colspan=\"2\" halign=\"left\">A</th>\n",
       "      <th colspan=\"2\" halign=\"left\">B</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th>sum</th>\n",
       "      <th>std</th>\n",
       "      <th>mean</th>\n",
       "      <th>std</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2012-01-01 00:00:00</th>\n",
       "      <td>-21.403502</td>\n",
       "      <td>0.990016</td>\n",
       "      <td>-0.062350</td>\n",
       "      <td>1.013514</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2012-01-01 00:03:00</th>\n",
       "      <td>3.500088</td>\n",
       "      <td>0.904340</td>\n",
       "      <td>0.036599</td>\n",
       "      <td>1.018236</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2012-01-01 00:06:00</th>\n",
       "      <td>7.324659</td>\n",
       "      <td>1.043424</td>\n",
       "      <td>0.032505</td>\n",
       "      <td>0.887138</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2012-01-01 00:09:00</th>\n",
       "      <td>-3.153064</td>\n",
       "      <td>1.001389</td>\n",
       "      <td>-0.025258</td>\n",
       "      <td>1.099908</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2012-01-01 00:12:00</th>\n",
       "      <td>2.908233</td>\n",
       "      <td>1.056619</td>\n",
       "      <td>-0.008374</td>\n",
       "      <td>1.045755</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2012-01-01 00:15:00</th>\n",
       "      <td>-1.699902</td>\n",
       "      <td>0.937193</td>\n",
       "      <td>0.128331</td>\n",
       "      <td>0.943251</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                             A                   B          \n",
       "                           sum       std      mean       std\n",
       "2012-01-01 00:00:00 -21.403502  0.990016 -0.062350  1.013514\n",
       "2012-01-01 00:03:00   3.500088  0.904340  0.036599  1.018236\n",
       "2012-01-01 00:06:00   7.324659  1.043424  0.032505  0.887138\n",
       "2012-01-01 00:09:00  -3.153064  1.001389 -0.025258  1.099908\n",
       "2012-01-01 00:12:00   2.908233  1.056619 -0.008374  1.045755\n",
       "2012-01-01 00:15:00  -1.699902  0.937193  0.128331  0.943251"
      ]
     },
     "execution_count": 211,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.agg({'A' : ['sum','std'], 'B' : ['mean','std'] })"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 213,
   "metadata": {
    "collapsed": false,
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>a</th>\n",
       "      <th>date</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>v</th>\n",
       "      <th>d</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <th>2015-01-04</th>\n",
       "      <td>0</td>\n",
       "      <td>2015-01-04</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <th>2015-01-11</th>\n",
       "      <td>1</td>\n",
       "      <td>2015-01-11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <th>2015-01-18</th>\n",
       "      <td>2</td>\n",
       "      <td>2015-01-18</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <th>2015-01-25</th>\n",
       "      <td>3</td>\n",
       "      <td>2015-01-25</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <th>2015-02-01</th>\n",
       "      <td>4</td>\n",
       "      <td>2015-02-01</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "              a       date\n",
       "v d                       \n",
       "1 2015-01-04  0 2015-01-04\n",
       "2 2015-01-11  1 2015-01-11\n",
       "3 2015-01-18  2 2015-01-18\n",
       "4 2015-01-25  3 2015-01-25\n",
       "5 2015-02-01  4 2015-02-01"
      ]
     },
     "execution_count": 213,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = pd.DataFrame({'date': pd.date_range('2015-01-01', freq='W', periods=5),\n",
    "                   'a': np.arange(5)},\n",
    "                  index=pd.MultiIndex.from_arrays([[1,2,3,4,5],\n",
    "                                                   pd.date_range('2015-01-01', freq='W',periods=5)],names=['v','d']))\n",
    "df"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "If a DataFrame does not have a datetimelike index, but instead you want to resample based on datetimelike column\n",
    "in the frame, it can passed to the on keyword."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 214,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>a</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>date</th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2015-01-31</th>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-02-28</th>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "            a\n",
       "date         \n",
       "2015-01-31  6\n",
       "2015-02-28  4"
      ]
     },
     "execution_count": 214,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.resample('M', on='date').sum()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Similarly, if you instead want to resample by a datetimelike level of MultiIndex, its name or location can be passed\n",
    "to the level keyword."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 215,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>a</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>d</th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2015-01-31</th>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-02-28</th>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "            a\n",
       "d            \n",
       "2015-01-31  6\n",
       "2015-02-28  4"
      ]
     },
     "execution_count": 215,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.resample('M', level='d').sum()"
   ]
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python [conda root]",
   "language": "python",
   "name": "conda-root-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
